Android SharedPreferences.getBoolean 每次都返回 true
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23934645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SharedPreferences.getBoolean returns true everytime
提问by Nima Sakhtemani
I made a class for handling important data changes such as App Purchase Status and other stuff .
我创建了一个类来处理重要的数据更改,例如 App Purchase Status 和其他东西。
For this goal I have created a class which does the setting and reading of the values. but the problem is whenever I call the appIsPurchased() method, the result is true while it hasen't been changed since app installation and its first initial launch.
为了这个目标,我创建了一个类来设置和读取值。但问题是,每当我调用 appIsPurchased() 方法时,结果都是 true,但自应用程序安装和首次启动以来没有更改。
This is my code:
这是我的代码:
/**
* Created by neemasa on 5/29/14.
* This class handles more crucial data values within app.
*/
public class AppCore {
private SharedPreferences settings;
private String keyPurchase = "app_purchased";
private Context context;
public AppCore(Context context){
this.context = context;
settings = PreferenceManager.getDefaultSharedPreferences(context);
}
public void setAppInPurchasedMode(String status){
if (status.equals("successful")){
settings.edit().putBoolean(keyPurchase, true).commit();
}else if (status.equals("failed")){
settings.edit().putBoolean(keyPurchase, false).commit();
}
}
public boolean appIsPurchased(){
boolean purchased = false;
if (settings.getBoolean(keyPurchase,true)){
purchased = true;
}
return purchased;
}
}
问题一:我的代码有问题吗?如果有那么为什么 appIsPurchased() 总是返回 true?
问题 2:默认情况下共享首选项中的所有值都为 true 吗?
Meanwhile when I use this class in my code the toast "Purchased!" runs even when app is running for the first time.
同时,当我在我的代码中使用这个类时,吐司“已购买!” 即使应用程序第一次运行也能运行。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCore appCore = new AppCore(getApplicationContext());
if (appCore.appIsPurchased()){
Toast.makeText(getApplicationContext(),"Purchased!",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(),"Not Purchased!",Toast.LENGTH_SHORT).show();
}
}
采纳答案by Nima Sakhtemani
Found It, the problem is that I was thinking
找到了,问题是我在想
settings.getBoolean(keyPurchase,false)
returns the value of keyPurchased variable but the fact is it only returns the variable itself not its value so I fixed the problem by changing the method of my class to this:
返回 keyPurchased 变量的值,但事实是它只返回变量本身而不是它的值,所以我通过将我的类的方法更改为这个来解决这个问题:
public boolean appIsPurchased(){
return settings.getBoolean(keyPurchase,false);
}
回答by George Thomas
Actually there is a problem in your code!! thats why its always showing purchased!!
其实你的代码有问题!!这就是为什么它总是显示已购买!
if (settings.getBoolean(keyPurchase,true)){
purchased = true;
}
in this lines if the keyPurchased tag if not used , u are passing true value by default so when u call
在这一行中,如果未使用 keyPurchased 标签,则默认情况下您将传递 true 值,因此当您调用时
if (appCore.appIsPurchased()){
it always return a true value.. The solution is that make sure that the preference values are set before u call them.. hope this helps
它总是返回一个真值.. 解决方案是确保在你调用它们之前设置偏好值..
回答by Blackbelt
you are setting the default value to true, so either your sharedpreference does not contains an entry for key_purchased or setAppInPurchasedMode
is never called or is called wit status successful
. On the minor side, your
您将默认值设置为 true,因此您的 sharedpreference 不包含 key_purchased 的条目,或者setAppInPurchasedMode
从未被调用或被称为 wit status successful
。在次要方面,您的
public boolean appIsPurchased(){
boolean purchased = false;
if (settings.getBoolean(keyPurchase,true)){
purchased = true;
}
return purchased;
}
can be implemented like:
可以实现如下:
public boolean appIsPurchased(){
return settings.getBoolean(keyPurchase, false);
}
about setAppInPurchasedMode
, if I were in you I would change the way you compare status
, this way:
关于setAppInPurchasedMode
,如果我在你里面,我会改变你比较的方式status
,这样:
public void setAppInPurchasedMode(String status){
if ("successful".equals(status)){
settings.edit().putBoolean(keyPurchase, true).commit();
} else if ("failed".equals(status)){
settings.edit().putBoolean(keyPurchase, false).commit();
}
}
the difference is that if status
is null, the way you implemented will crash your application with NPE. With my implementation you'll get false, because "successful" instanceof
null is always false, and instanceof
is the first check for equals
不同之处在于,如果 status
为空,则您实现的方式将使您的应用程序因 NPE 崩溃。使用我的实现你会得到错误,因为"successful" instanceof
null 总是错误的,并且instanceof
是第一个检查equals
回答by Ruan
For those still having a problem, remember to apply the changes to your preferences.
对于那些仍然有问题的人,请记住将更改应用于您的首选项。
private SharedPreferences sharedPreferences ;
private SharedPreferences.Editor sharedPreferencesEditor;
sharedPreferencesEditor.putBoolean("myVariable", false);
sharedPreferencesEditor.apply();