MyApplication.java
1.49 KB
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
package cn.banxe.bxe;
import android.content.Context;
import android.content.SharedPreferences;
import com.tencent.chat.flutter.push.tencent_cloud_chat_push.application.TencentCloudChatPushApplication;
public class MyApplication extends TencentCloudChatPushApplication {
private static final String PREFS_NAME = "privacy_prefs";
private static final String KEY_AGREED = "privacy_agreed";
private static MyApplication instance;
@Override
public void onCreate() {
instance = this;
// 只有在用户已同意隐私政策时才执行初始化
if (isPrivacyAgreed()) {
super.onCreate();
}
}
private boolean isPrivacyAgreed() {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
return prefs.getBoolean(KEY_AGREED, false);
}
/**
* 用户同意隐私政策后调用,执行延迟初始化
*/
public static void onPrivacyAgreed() {
if (instance != null && !instance.isPrivacyAgreed()) {
// 先保存同意状态
instance.savePrivacyAgreed();
// 再执行初始化
instance.initSDKs();
}
}
private void savePrivacyAgreed() {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
prefs.edit().putBoolean(KEY_AGREED, true).apply();
}
private void initSDKs() {
// 执行父类的初始化逻辑v
super.onCreate();
}
}