Android存储-MMKV的使用

MMKV是腾讯开源的存储键值对的框架

1、 导入依赖库

// build.gradle mmkv:键值对存储
implementation 'com.tencent:mmkv-static:1.0.23'

// relinker 加载lib包,用于解决Android 8.1 mmkv so库找不到问题
implementation 'com.getkeepsafe.relinker:relinker:1.4.1'

2、 在Application中初始化

新建MmkvTestApplication类,然后在你的AndroidManifest.xml中,指定Application

<application
    android:name=".MmkvTestApplication">

MmKvTestApplication中初始化MMKV

protected void init() {
    loadMMkvLib();
    // 将SP 迁移到 MMKV
    transformDataToMMkv();
}

private void loadMMkvLib() {
    Log.d(TAG, "load MMkv lib");
    // 解决在Android 8.1上 mmkv so库找不到问题
    if (Build.VERSION.SDK_INT == VersionCodes.O_MR1) {
        String dir = getFilesDir().getAbsolutePath() + MMKV_PATH;
        MMKV.initialize(dir, libName -> ReLinker.loadLibrary(CoreApplication.this, libName));
        return;
    }
    // 初始化mmkv,数据默认存储在/data/data/packageName/files/mmkv
    MMKV.initialize(this);
}

@Override
protected void transformDataToMMkv() {
    super.transformDataToMMkv();
    // 将SP数据 迁移到 mmkv, 如CacheSp内容
    if ((!CacheSp.getInstance().getSharedPreferences().getAll().isEmpty()) && CacheSp.getInstance().getMMKV().allKeys() == null) {
        CacheSp.getInstance().transform();
    }
}

3、 封装MMKV基本的操作

public class BaseMmkv {
    private SharedPreferences mSharedPreferences;
    private MMKV mMMKV;
    private String mSharedPreferencesFileName;

    public void init(Context context, String sharedPreferencesFileName) {
        if (TextUtils.isEmpty(sharedPreferencesFileName)) {
            throw new RuntimeException("sharedPreferencesFileName isn't null");
        }
        mMMKV = MMKV.mmkvWithID(sharedPreferencesFileName);
        mSharedPreferences = context.getSharedPreferences(sharedPreferencesFileName, 0);
        mSharedPreferencesFileName = sharedPreferencesFileName;
    }

    public SharedPreferences getSharedPreferences() {
        return this.mSharedPreferences;
    }

    public MMKV getMMKV() {
        return mMMKV;
    }

    // mmkv 的第一种写法,在官方readMe介绍上,mmkv所有变更立马生效,无需使用apply、sync
    public void putString(String key, String value) {
        SharedPreferences.Editor editor = mMMKV.edit();
        editor.putString(key, value);
        editor.apply();
    }

    // mmkv 的第二种写法,其实和第一种本质是相同的
    public void putStringV2(String key, String value) {
        mMMKV.encode(key, value);
    }

    public String getString(String key, String defaultValue) {
        if (this.mMMKV == null) {
            throw new RuntimeException("sharedPreferencesFileName isn't null");
        }
        return this.mMMKV.getString(key, defaultValue);
    }

    public String getStringV2(String key, String defaultValue) {
        if (this.mMMKV == null) {
            throw new RuntimeException("sharedPreferencesFileName isn't null");
        }
        return this.mMMKV.decodeString(key, defaultValue);
    }

    // 将sp内容 迁移到 mmkv 中
    public void transform() {
        if (!TextUtils.isEmpty(mSharedPreferencesFileName)) {
            MMKV mmkv = MMKV.mmkvWithID(mSharedPreferencesFileName);
            mmkv.importFromSharedPreferences(mSharedPreferences);
            mSharedPreferences.edit().clear().apply();
        }
    }
}

4、 以模块使用来创建,如CacheSp

public class CacheSp extends BaseMmkv {

    private static final String SHARED_PREFS_FILE_NAME = "com.qlli.cache";

    private static final Singleton<CacheSp> mInstance = new Singleton<CacheSp>() {
        @Override
        protected CacheSp newInstance() {
            return new CacheSp();
        }
    };

    private CacheSp() {
        init(BaseApplication.getBaseApplication(), SHARED_PREFS_FILE_NAME);
    }

    public static CacheSp getInstance() {
        return mInstance.getInstance();
    }
}

// 单列模式
public abstract class Singleton<T> {

    private volatile T mInstance;

    protected abstract T newInstance();

    public final T getInstance() {
        if (mInstance == null) {
            synchronized (this) {
                if (mInstance == null) {
                    mInstance = newInstance();
                }
            }
        }
        return mInstance;
    }

    public final void releaseInstance() {
        synchronized (this) {
            if (mInstance != null) {
                mInstance = null;
            }
        }
    }
}

5、 使用

// 存k-v值
CacheSp.getInstance().putStringV2("name","Tom");
// 取k-v值
textView.setText(CacheSp.getInstance().getStringV2("name", "qlli"));

6、 存储的文件保存位置在:/data/data//files/mmkv/ 目录下,然后以你的具体传入文件名参数,在该目录下创建两个文件,一个是保存数据的文件,一个是校验文件以crc后缀名结尾

results matching ""

    No results matching ""