这个性能非常厉害。

依赖

1
2
3
//腾讯开源的高性能keyValue存储,用来替代系统的SharedPreferences
//https://github.com/Tencent/MMKV
implementation 'com.tencent:mmkv-static:+'

初始化

MainActivity旁加一个AppContext

意思为整个app的一些配置,只会在app启动时候运行一次。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import android.app.Application;

import com.tencent.mmkv.MMKV;


/**
* 全局Application
* 整个生命周期只执行一次
*/
public class AppContext extends Application {
@Override
public void onCreate() {
super.onCreate();
initMMKV();
}
/**
* 初始化 腾讯开源的高性能keyValue存储,用来替代系统的SharedPreferences
*/
private void initMMKV() {
String rootDir = MMKV.initialize(this);

}
}

然后还要去配置文件中设置 App 的 name

image-20230530101638129

封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.tencent.mmkv.MMKV;

/**
* 偏好设置工具类MMKV
*/
public class PreferenceUtil {

/**
* 偏好设置文件名称
*/

private static PreferenceUtil instance;
private final Context context;
private final MMKV preference;
/**
* 是否显示引导界面key
*/
private static final String SHOW_GUIDE = "SHOW_GUIDE";

//region basicsetting
/**
* 构造方法
*
* @param context
*/
public PreferenceUtil(Context context) {
//保存上下文(getApplicationContext代表整个app,就脱离activity。避免内存泄漏)
this.context = context.getApplicationContext();
preference = MMKV.defaultMMKV();
}

/**
* 获取偏好设置单例
*
* @param context
* @return
*/
public synchronized static PreferenceUtil getInstance(Context context) {
//保存上下文
if (instance == null) {
instance = new PreferenceUtil(context);
}
return instance;
}
//endregion

/**
* 是否显示引导界面
*
* @return
*/
public boolean isShowGuide() {
return getBoolean(SHOW_GUIDE, true);
}

/**
* 设置是否显示引导界面
*
* @param value
*/
public void setShowGuide(boolean value) {
putBoolean(SHOW_GUIDE, value);
}


//region help function
/**
* 保存boolean
*
* @param key
* @param value
*/
private void putBoolean(String key, boolean value) {
preference.edit().putBoolean(key, value).apply();
}

/**
* 获取boolean
*
* @param key
* @param defaultValue
* @return
*/
private boolean getBoolean(String key, boolean defaultValue) {
return preference.getBoolean(key, defaultValue);
}
//endregion

}

使用

1
2
3
4
5
6
7
private void prepareNext() {
if (PreferenceUtil.getInstance(getHostActivity()).isShowGuide()){
startActivityAfterFinishThis(GuideActivity.class);
return;
}
postNext();
}

然后记得在有些地方去设置这个偏好设置为false。

就可以正常工作了。

优化

每次写PreferenceUtil.getInstance(getHostActivity())太重复了。

我们去BaseLogicActivity(这个项目APP通用逻辑)里面。

1
2
3
4
5
6
7
protected PreferenceUtil sp;

@Override
protected void initDatum() {
super.initDatum();
sp = PreferenceUtil.getInstance(getHostActivity());
}

然后要用偏好设置的Activity 继承这个BaseLogicActivity 然后就可以改成成这样:

1
2
3
4
5
6
7
private void prepareNext() {
if (sp.isShowGuide()){
startActivityAfterFinishThis(GuideActivity.class);
return;
}
postNext();
}

成果展示

理论逻辑是:

第一来

splash界面然后用户协议,然后动态授权后去guide界面

第二次进入就是 splash 直接去main界面

所以我们以后就用腾讯的这个MMKV来代替原生的Preference