引入依赖版本
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());
}
@Override
public IMPL save(@NonNull String key, @NonNull Parcelable value) {
MMKV.defaultMMKV()
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String key, @NonNull Set<String> value) {
MMKV.defaultMMKV()
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String key, @NonNull byte[] value) {
MMKV.defaultMMKV()
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String name, @NonNull String key, @NonNull Parcelable value) {
MMKV.mmkvWithID(name)
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String name, @NonNull String key, @NonNull Set<String> value) {
MMKV.mmkvWithID(name)
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String name, @NonNull String key, @NonNull byte[] value) {
MMKV.mmkvWithID(name)
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL remove(@NonNull String[] keyArr) {
MMKV.defaultMMKV().removeValuesForKeys(keyArr);
return (IMPL) this;
}
@Override
public IMPL remove(@NonNull String name,@NonNull String[] keyArr) {
MMKV.mmkvWithID(name).removeValuesForKeys(keyArr);
return (IMPL) this;
}
@Override
public <T extends Parcelable> T getParcelable(@NonNull String key,Class<T> tClass) {
return MMKV.defaultMMKV().decodeParcelable(key,tClass);
}
@Override
public Set<String> getStringSet(@NonNull String key) {
return MMKV.defaultMMKV().decodeStringSet(key);
}
@Override
public byte[] getByteArray(@NonNull String key) {
return MMKV.defaultMMKV().decodeBytes(key);
}
@Override
public <T extends Parcelable> T getParcelable(@NonNull String name, @NonNull String key,Class<T> tClass) {
return MMKV.mmkvWithID(name).decodeParcelable(key,tClass);
}
@Override
public Set<String> getStringSet(@NonNull String name, @NonNull String key) {
return MMKV.mmkvWithID(name).decodeStringSet(key);
}
@Override
public byte[] getByteArray(@NonNull String name, @NonNull String key) {
return MMKV.mmkvWithID(name).decodeBytes(key);
}
@Override
public IMPL save(@NonNull String key, String value) {
MMKV.defaultMMKV()
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String key, int value) {
MMKV.defaultMMKV()
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String key, double value) {
MMKV.defaultMMKV()
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String key, boolean value) {
MMKV.defaultMMKV()
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String key, long value) {
MMKV.defaultMMKV()
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String key, float value) {
MMKV.defaultMMKV()
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String name, @NonNull String key, String value) {
MMKV.mmkvWithID(name)
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String name, @NonNull String key, int value) {
MMKV.mmkvWithID(name)
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String name, @NonNull String key, double value) {
MMKV.mmkvWithID(name)
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String name, @NonNull String key, boolean value) {
MMKV.mmkvWithID(name)
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String name, @NonNull String key, long value) {
MMKV.mmkvWithID(name)
.encode(key,value);
return (IMPL) this;
}
@Override
public IMPL save(@NonNull String name, @NonNull String key, float value) {
MMKV.mmkvWithID(name)
.encode(key,value);
return (IMPL) this;
}
@Override
public String getString(@NonNull String key) {
return MMKV.defaultMMKV().decodeString(key,"");
}
@Override
public int getInteger(@NonNull String key) {
return MMKV.defaultMMKV().decodeInt(key);
}
@Override
public double getDouble(@NonNull String key) {
return MMKV.defaultMMKV().decodeDouble(key);
}
@Override
public boolean getBoolean(@NonNull String key) {
return MMKV.defaultMMKV().decodeBool(key);
}
@Override
public long getLong(@NonNull String key) {
return MMKV.defaultMMKV().decodeLong(key);
}
@Override
public float getFloat(@NonNull String key) {
return MMKV.defaultMMKV().decodeFloat(key);
}
@Override
public String getString(@NonNull String name, @NonNull String key) {
return MMKV.mmkvWithID(name).decodeString(key,"");
}
@Override
public int getInteger(@NonNull String name, @NonNull String key) {
return MMKV.mmkvWithID(name).decodeInt(key);
}
@Override
public double getDouble(@NonNull String name, @NonNull String key) {
return MMKV.mmkvWithID(name).decodeDouble(key);
}
@Override
public boolean getBoolean(@NonNull String name, @NonNull String key) {
return MMKV.mmkvWithID(name).decodeBool(key);
}
@Override
public long getLong(@NonNull String name, @NonNull String key) {
return MMKV.mmkvWithID(name).decodeLong(key);
}
@Override
public float getFloat(@NonNull String name, @NonNull String key) {
return MMKV.mmkvWithID(name).decodeFloat(key);
}
@Override
public String getStringWithDefault(@NonNull String key, String defaultValue) {
return MMKV.defaultMMKV().decodeString(key,defaultValue);
}
@Override
public int getIntegerWithDefault(@NonNull String key, int defaultValue) {
return MMKV.defaultMMKV().decodeInt(key,defaultValue);
}
@Override
public double getDoubleWithDefault(@NonNull String key, double defaultValue) {
return MMKV.defaultMMKV().decodeDouble(key,defaultValue);
}
@Override
public boolean getBooleanWithDefault(@NonNull String key, boolean defaultValue) {
return MMKV.defaultMMKV().decodeBool(key,defaultValue);
}
@Override
public long getLongWithDefault(@NonNull String key, long defaultValue) {
return MMKV.defaultMMKV().decodeLong(key,defaultValue);
}
@Override
public float getFloatWithDefault(@NonNull String key, float defaultValue) {
return MMKV.defaultMMKV().decodeFloat(key,defaultValue);
}
@Override
public String getStringWithDefault(@NonNull String name, @NonNull String key, String defaultValue) {
return MMKV.mmkvWithID(name).decodeString(key,defaultValue);
}
@Override
public int getIntegerWithDefault(@NonNull String name, @NonNull String key, int defaultValue) {
return MMKV.mmkvWithID(name).decodeInt(key,defaultValue);
}
@Override
public double getDoubleWithDefault(@NonNull String name, @NonNull String key, double defaultValue) {
return MMKV.mmkvWithID(name).decodeDouble(key,defaultValue);
}
@Override
public boolean getBooleanWithDefault(@NonNull String name, @NonNull String key, boolean defaultValue) {
return MMKV.mmkvWithID(name).decodeBool(key,defaultValue);
}
@Override
public long getLongWithDefault(@NonNull String name, @NonNull String key, long defaultValue) {
return MMKV.mmkvWithID(name).decodeLong(key,defaultValue);
}
@Override
public float getFloatWithDefault(@NonNull String name, @NonNull String key, float defaultValue) {
return MMKV.mmkvWithID(name).decodeFloat(key,defaultValue);
}
@Override
public IMPL remove(String key) {
MMKV.defaultMMKV().remove(key);
return (IMPL) this;
}
@Override
public 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)
|