我目前正在尝试在手机启动时基于布尔值 false 或 true 启动服务。问题是如果我使用 getBoolean
boolean isPhysicalSirenFlagged = sp.getBoolean("isPhysicalSirenFlagged", true);
boolean isSMSSirenFlagged = sp.getBoolean("isSMSSirenFlagged", true);
每当手机启动时,它们都会设置为 true,导致我的 isPhysicalSirenFlagged 和 isSMSSirenFlagged 都为 true。是否可以检查值的当前布尔值是什么?
代码:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
String value = sp.getString("serial", "");
boolean isPhysicalSirenFlagged = sp.getBoolean("isPhysicalSirenFlagged", true);
boolean isSMSSirenFlagged = sp.getBoolean("isSMSSirenFlagged", true);
if (isPhysicalSirenFlagged) {
//true
Intent physicaldialog = new Intent(context, PhysicalTheftDialog.class);
physicaldialog.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(physicaldialog);
context.startService(new Intent(context, PhysicalTheftService.class));
}
else {
//false
}
if (isSMSSirenFlagged) {
//true
Intent smsdialog = new Intent(context, SMSNotificationPasswordDialog.class);
smsdialog.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(smsdialog);
context.startService(new Intent(context, RemoteSirenService.class));
}
else {
//false
}