问题与共享preferences布尔 [英] Issue with boolean on shared preferences

查看:116
本文介绍了问题与共享preferences布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在试图启动基于布尔或真或假的时候,手机开机的服务。问题是如果我使用getBoolean

 布尔isPhysicalSirenFlagged = sp.getBoolean(isPhysicalSirenFlagged,真正的);
 布尔isSMSSirenFlagged = sp.getBoolean(isSMSSirenFlagged,真正的);

他们都将设置为true,只要手机开机导致我我的两个isPhysicalSirenFlagged&安培; isSMSSirenFlagged是真实的时候。是否有可能要检查什么是价值目前布尔?

code:

 共享preferences SP = preferenceManager.getDefaultShared preferences(背景);
     字符串值= sp.getString(串行,);
     布尔isPhysicalSirenFlagged = sp.getBoolean(isPhysicalSirenFlagged,真正的);
     布尔isSMSSirenFlagged = sp.getBoolean(isSMSSirenFlagged,真正的);     如果(isPhysicalSirenFlagged){
         //真正
         意图physicaldialog =新意图(背景下,PhysicalTheftDialog.class);
         physicaldialog.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(physicaldialog);
         context.startService(新意图(上下文,PhysicalTheftService.class));
     }
     其他{
         //假
     }     如果(isSMSSirenFlagged){
         //真正
         意图smsdialog =新意图(背景下,SMSNotificationPasswordDialog.class);
         smsdialog.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(smsdialog);
         context.startService(新意图(上下文,RemoteSirenService.class));
     }
     其他{
         //假
     }


解决方案

有它设置为false,而不是,明明什么都你正在做不commiting更改共享prefereces指示标志。通过使用假作为默认,你将prevent偶然真正分配给标志。说实话,你应该用INT标志(或preferred枚举)重新present这一点。它是确定设备是在该州的一个更安全的方式,例如:

  INT NO_STATE = 0
INT IS_THEFTED = 1
INT IS_WAILING = 2要么枚举国家{
    NONE,THEFTED,WAILING;
}

I am currently trying to start a service based on the boolean false or true when the phone boots up. The issue is if i use getBoolean

 boolean isPhysicalSirenFlagged = sp.getBoolean("isPhysicalSirenFlagged", true);
 boolean isSMSSirenFlagged = sp.getBoolean("isSMSSirenFlagged", true);

Both of them will get set to true whenever the phone boots up causing my both my isPhysicalSirenFlagged & isSMSSirenFlagged to be true when. Is it possible to check what is the current boolean of a value?

Code:

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
     }

解决方案

Have it set to false instead, obviously what ever you are doing is not commiting a change to the SharedPrefereces to indicate the flag. By using false as a default, you will prevent the accidental true assignment to the flags. Truthfully, you should be using int flags (or preferred an enum) to represent this. It is a much safer way of determining the state the device is in. for example:

int NO_STATE    = 0
int IS_THEFTED  = 1
int IS_WAILING  = 2

or

enum State {
    NONE, THEFTED, WAILING;
}

这篇关于问题与共享preferences布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆