这是我的按钮布尔保存源
private static SharedPreferences mPrefs;
private static SharedPreferences.Editor mPrefsEditor;
private static final String noti = "pref_key_noti_onoff";
public static void initPrefs(Context context) {
if( mPrefs == null){
mPrefs = context.getSharedPreferences(context.getPackageName() + "_preferences", 0);
mPrefsEditor = mPrefs.edit();
}
}
public static boolean getSettingnoti() {
return mPrefs.getBoolean(noti,true);
}
public static void setSettingnoti(boolean onoff) {
mPrefsEditor = mPrefs.edit();
mPrefsEditor.putBoolean(noti,onoff);
mPrefsEditor.commit();
}
这是我的fcm收到来源
public void onMessageReceived(RemoteMessage remoteMessage) {
if (DataSaver.getSettingnoti()){}
else{
mPrefsEditor = mPrefs.edit();
mPrefsEditor.putBoolean(noti,true);
mPrefsEditor.commit();
}
if (DataSaver.getSettingnoti() == false) {
Log.d(TAG, "onMessageReceived: ");
} else {
Integer.parseInt(DataSaver.getSettingsttime());
Integer.parseInt(DataSaver.getSettingedtime());
Log.d(TAG, "onMessageReceived:" + Integer.parseInt(DataSaver.getSettingsttime()) + Integer.parseInt(DataSaver.getSettingedtime()));
sendPushNotification(remoteMessage.getData().get("message"));
set_alarm_badge();
}
}
答案 0 :(得分:0)
在使用之前,您需要修改此代码并对对象执行空检查。
public static boolean getSettingnoti() {
if(mPrefs != null)
return mPrefs.getBoolean(noti,true);
/* put some log here */
return false;
}