共享preferences.getBoolean返回true,每次 [英] SharedPreferences.getBoolean returns true everytime
问题描述
我做了一个类来处理重要数据的变化,如App Purchase的状态和其他的东西。
有关这一目标,我创建了一个类,它是否值的设置和阅读。但问题是,每当我所说的appIsPurchased()方法,而它hasen't以来安装的应用程序和它的第一个首次推出改变了,结果是真实的。
这是我的code:
/ **
*创建者neemasa于14年5月29日。
*此类处理应用程序中比较关键的数据值。
* /
公共类AppCore { 私人共享preferences设置;
私人字符串keyPurchase =app_purchased;
私人上下文的背景下; 公共AppCore(上下文的背景下){
this.context =背景;
设置= preferenceManager.getDefaultShared preferences(背景);
} 公共无效setAppInPurchasedMode(字符串状态){
如果(status.equals(成功)){
。settings.edit()putBoolean(keyPurchase,真).commit();
}否则如果(status.equals(失败)){
。settings.edit()putBoolean(keyPurchase,FALSE).commit();
} }
公共布尔appIsPurchased(){
布尔购买= FALSE;
如果(settings.getBoolean(keyPurchase,真)){
购买= TRUE;
}
返回购买;
}}
问题1:是有什么错我的code?如果有,那么为什么appIsPurchased()始终返回true?
第二个问题:做共享preferences默认情况下真正的全值?
此外,当我使用这个类在我的code敬酒购买!运行,即使应用程序正在运行的第一次。
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); AppCore appCore =新AppCore(getApplicationContext());
如果(appCore.appIsPurchased()){
Toast.makeText(getApplicationContext(),购买!,Toast.LENGTH_SHORT).show();
}其他{
Toast.makeText(getApplicationContext(),没有购买!,Toast.LENGTH_SHORT).show();
}
}
找到了,问题是,我想
settings.getBoolean(keyPurchase,FALSE)
返回keyPurchased变量的值,但事实上它只返回变量本身没有它的价值,所以我改变我类的方法来修复这个问题:
公共布尔appIsPurchased(){
返回settings.getBoolean(keyPurchase,FALSE);
}
I made a class for handling important data changes such as App Purchase Status and other stuff .
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.
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;
}
}
Question 1st: is there something wrong with my code? if there is then why appIsPurchased() always return true?
Question 2nd: do all values in the shared preferences are true by default?
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();
}
}
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:
public boolean appIsPurchased(){
return settings.getBoolean(keyPurchase,false);
}
这篇关于共享preferences.getBoolean返回true,每次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!