我目前正试图在手机启动时根据布尔值false或true启动服务。问题是如果我使用getBoolean关于共享首选项的布尔值问题
boolean isPhysicalSirenFlagged = sp.getBoolean("isPhysicalSirenFlagged", true);
boolean isSMSSirenFlagged = sp.getBoolean("isSMSSirenFlagged", true);
他们都将设置为true,每当电话启动导致我的我的两个isPhysicalSirenFlagged & isSMSSirenFlagged是真实的时候。是否有可能检查一个值的当前布尔值是什么?
代码:
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
}
我不明白您的问题。 如果您将它们设置为false,它们将是错误的。 – 2012-08-07 15:11:00
是的,这就是为什么我想知道,而不是将它们设置为false有没有办法来检查当前可能isSMSSIrenFlagged的布尔值?因为无论手机何时启动,都会执行getBoolean语句 – dythe 2012-08-07 15:12:37
@dythe否*。这就是为什么有一个默认值(您作为第二个参数传入的真实值)。 – AedonEtLIRA 2012-08-07 15:15:20