引入依赖版本
implementation 'com.tencent:mmkv-static:1.2.11'
public class TencentMMKV<IMPL extends StoreBox> implements MMKVBehavior<IMPL> {private static TencentMMKV self;private TencentMMKV(){}public static TencentMMKV getInstance(){if(self == null){self = new TencentMMKV();}return self ;}public static void init(Context context) {String rootDir = MMKV.initialize(context.getApplicationContext());}@Overridepublic IMPL save(@NonNull String key, @NonNull Parcelable value) {MMKV.defaultMMKV().encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String key, @NonNull Set<String> value) {MMKV.defaultMMKV().encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String key, @NonNull byte[] value) {MMKV.defaultMMKV().encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String name, @NonNull String key, @NonNull Parcelable value) {MMKV.mmkvWithID(name).encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String name, @NonNull String key, @NonNull Set<String> value) {MMKV.mmkvWithID(name).encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String name, @NonNull String key, @NonNull byte[] value) {MMKV.mmkvWithID(name).encode(key,value);return (IMPL) this;}@Overridepublic IMPL remove(@NonNull String[] keyArr) {MMKV.defaultMMKV().removeValuesForKeys(keyArr);return (IMPL) this;}@Overridepublic IMPL remove(@NonNull String name,@NonNull String[] keyArr) {MMKV.mmkvWithID(name).removeValuesForKeys(keyArr);return (IMPL) this;}@Overridepublic <T extends Parcelable> T getParcelable(@NonNull String key,Class<T> tClass) {return MMKV.defaultMMKV().decodeParcelable(key,tClass);}@Overridepublic Set<String> getStringSet(@NonNull String key) {return MMKV.defaultMMKV().decodeStringSet(key);}@Overridepublic byte[] getByteArray(@NonNull String key) {return MMKV.defaultMMKV().decodeBytes(key);}@Overridepublic <T extends Parcelable> T getParcelable(@NonNull String name, @NonNull String key,Class<T> tClass) {return MMKV.mmkvWithID(name).decodeParcelable(key,tClass);}@Overridepublic Set<String> getStringSet(@NonNull String name, @NonNull String key) {return MMKV.mmkvWithID(name).decodeStringSet(key);}@Overridepublic byte[] getByteArray(@NonNull String name, @NonNull String key) {return MMKV.mmkvWithID(name).decodeBytes(key);}@Overridepublic IMPL save(@NonNull String key, String value) {MMKV.defaultMMKV().encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String key, int value) {MMKV.defaultMMKV().encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String key, double value) {MMKV.defaultMMKV().encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String key, boolean value) {MMKV.defaultMMKV().encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String key, long value) {MMKV.defaultMMKV().encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String key, float value) {MMKV.defaultMMKV().encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String name, @NonNull String key, String value) {MMKV.mmkvWithID(name).encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String name, @NonNull String key, int value) {MMKV.mmkvWithID(name).encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String name, @NonNull String key, double value) {MMKV.mmkvWithID(name).encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String name, @NonNull String key, boolean value) {MMKV.mmkvWithID(name).encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String name, @NonNull String key, long value) {MMKV.mmkvWithID(name).encode(key,value);return (IMPL) this;}@Overridepublic IMPL save(@NonNull String name, @NonNull String key, float value) {MMKV.mmkvWithID(name).encode(key,value);return (IMPL) this;}@Overridepublic String getString(@NonNull String key) {return MMKV.defaultMMKV().decodeString(key,"");}@Overridepublic int getInteger(@NonNull String key) {return MMKV.defaultMMKV().decodeInt(key);}@Overridepublic double getDouble(@NonNull String key) {return MMKV.defaultMMKV().decodeDouble(key);}@Overridepublic boolean getBoolean(@NonNull String key) {return MMKV.defaultMMKV().decodeBool(key);}@Overridepublic long getLong(@NonNull String key) {return MMKV.defaultMMKV().decodeLong(key);}@Overridepublic float getFloat(@NonNull String key) {return MMKV.defaultMMKV().decodeFloat(key);}@Overridepublic String getString(@NonNull String name, @NonNull String key) {return MMKV.mmkvWithID(name).decodeString(key,"");}@Overridepublic int getInteger(@NonNull String name, @NonNull String key) {return MMKV.mmkvWithID(name).decodeInt(key);}@Overridepublic double getDouble(@NonNull String name, @NonNull String key) {return MMKV.mmkvWithID(name).decodeDouble(key);}@Overridepublic boolean getBoolean(@NonNull String name, @NonNull String key) {return MMKV.mmkvWithID(name).decodeBool(key);}@Overridepublic long getLong(@NonNull String name, @NonNull String key) {return MMKV.mmkvWithID(name).decodeLong(key);}@Overridepublic float getFloat(@NonNull String name, @NonNull String key) {return MMKV.mmkvWithID(name).decodeFloat(key);}@Overridepublic String getStringWithDefault(@NonNull String key, String defaultValue) {return MMKV.defaultMMKV().decodeString(key,defaultValue);}@Overridepublic int getIntegerWithDefault(@NonNull String key, int defaultValue) {return MMKV.defaultMMKV().decodeInt(key,defaultValue);}@Overridepublic double getDoubleWithDefault(@NonNull String key, double defaultValue) {return MMKV.defaultMMKV().decodeDouble(key,defaultValue);}@Overridepublic boolean getBooleanWithDefault(@NonNull String key, boolean defaultValue) {return MMKV.defaultMMKV().decodeBool(key,defaultValue);}@Overridepublic long getLongWithDefault(@NonNull String key, long defaultValue) {return MMKV.defaultMMKV().decodeLong(key,defaultValue);}@Overridepublic float getFloatWithDefault(@NonNull String key, float defaultValue) {return MMKV.defaultMMKV().decodeFloat(key,defaultValue);}@Overridepublic String getStringWithDefault(@NonNull String name, @NonNull String key, String defaultValue) {return MMKV.mmkvWithID(name).decodeString(key,defaultValue);}@Overridepublic int getIntegerWithDefault(@NonNull String name, @NonNull String key, int defaultValue) {return MMKV.mmkvWithID(name).decodeInt(key,defaultValue);}@Overridepublic double getDoubleWithDefault(@NonNull String name, @NonNull String key, double defaultValue) {return MMKV.mmkvWithID(name).decodeDouble(key,defaultValue);}@Overridepublic boolean getBooleanWithDefault(@NonNull String name, @NonNull String key, boolean defaultValue) {return MMKV.mmkvWithID(name).decodeBool(key,defaultValue);}@Overridepublic long getLongWithDefault(@NonNull String name, @NonNull String key, long defaultValue) {return MMKV.mmkvWithID(name).decodeLong(key,defaultValue);}@Overridepublic float getFloatWithDefault(@NonNull String name, @NonNull String key, float defaultValue) {return MMKV.mmkvWithID(name).decodeFloat(key,defaultValue);}@Overridepublic IMPL remove(String key) {MMKV.defaultMMKV().remove(key);return (IMPL) this;}@Overridepublic IMPL remove(String name, String key) {MMKV.mmkvWithID(name).remove(key);return (IMPL) this;}}
public interface MMKVBehavior<IMPL extends MMKVBehavior> extends StoreBaseBehavior<IMPL> {IMPL save(@NonNull String key, @NonNull Parcelable value);IMPL save(@NonNull String key, @NonNull Set<String> value);IMPL save(@NonNull String key, @NonNull byte[] value);IMPL save(@NonNull String name, @NonNull String key, @NonNull Parcelable value);IMPL save(@NonNull String name, @NonNull String key, @NonNull Set<String> value);IMPL save(@NonNull String name, @NonNull String key, @NonNull byte[] value);IMPL remove(@NonNull String[] keyArr);IMPL remove(@NonNull String name, @NonNull String[] keyArr);<T extends Parcelable> T getParcelable(@NonNull String key, Class<T> tClass);Set<String> getStringSet(@NonNull String key);byte[] getByteArray(@NonNull String key);<T extends Parcelable> T getParcelable(@NonNull String name, @NonNull String key, Class<T> tClass);Set<String> getStringSet(@NonNull String name, @NonNull String key);byte[] getByteArray(@NonNull String name, @NonNull String key);}
public interface StoreBaseBehavior<IMPL extends StoreBaseBehavior> {IMPL save(@NonNull String key, String value);IMPL save(@NonNull String key, int value);IMPL save(@NonNull String key, double value);IMPL save(@NonNull String key, boolean value);IMPL save(@NonNull String key, long value);IMPL save(@NonNull String key, float value);IMPL save(@NonNull String name, @NonNull String key, String value);IMPL save(@NonNull String name, @NonNull String key, int value);IMPL save(@NonNull String name, @NonNull String key, double value);IMPL save(@NonNull String name, @NonNull String key, boolean value);IMPL save(@NonNull String name, @NonNull String key, long value);IMPL save(@NonNull String name, @NonNull String key, float value);String getString(@NonNull String key);int getInteger(@NonNull String key);double getDouble(@NonNull String key);boolean getBoolean(@NonNull String key);long getLong(@NonNull String key);float getFloat(@NonNull String key);String getString(@NonNull String name, @NonNull String key);int getInteger(@NonNull String name, @NonNull String key);double getDouble(@NonNull String name, @NonNull String key);boolean getBoolean(@NonNull String name, @NonNull String key);long getLong(@NonNull String name, @NonNull String key);float getFloat(@NonNull String name, @NonNull String key);String getStringWithDefault(@NonNull String key, String defaultValue);int getIntegerWithDefault(@NonNull String key, int defaultValue);double getDoubleWithDefault(@NonNull String key, double defaultValue);boolean getBooleanWithDefault(@NonNull String key, boolean defaultValue);long getLongWithDefault(@NonNull String key, long defaultValue);float getFloatWithDefault(@NonNull String key, float defaultValue);String getStringWithDefault(@NonNull String name, @NonNull String key, String defaultValue);int getIntegerWithDefault(@NonNull String name, @NonNull String key, int defaultValue);double getDoubleWithDefault(@NonNull String name, @NonNull String key, double defaultValue);boolean getBooleanWithDefault(@NonNull String name, @NonNull String key, boolean defaultValue);long getLongWithDefault(@NonNull String name, @NonNull String key, long defaultValue);float getFloatWithDefault(@NonNull String name, @NonNull String key, float defaultValue);IMPL remove(String key);IMPL remove(String name, String key);}
初始化
TencentMMKV.init(this)