引入依赖
// MMKV
implementation 'com.tencent:mmkv-static:1.2.10'
新建一个App类继承自Application并初始化MMKV
class App:Application(){ override fun onCreate() { super.onCreate() MMKV.initialize(this) // 初始化 } companion object{ const val TAG = "Application" } }
在Menifest中引用App
<application android:name=".App" // 此处引入 android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.CASDashboard"/>
使用示例
class TestMMKV{ private val mk: MMKV = MMKV.defaultMMKV() fun test(){ mk.encode("key","value") // 存一个String类型数据 val values = mk.decodeString("key") // 取一个String类型数据 mk.encode("key1",true) // 存一个Bool类型数据 val values1 = mk.decodeBool("key1") // 取一个Bool类型数据 mk.encode("key2",1) // 存一个Int类型数据 val values2 = mk.decodeInt("key2") // 取一个Int类型数据 } }