相关疑难解决方法(0)

SharedPreferences.getBoolean每次都返回true

我创建了一个类来处理重要的数据更改,例如App Purchase Status和其他东西.

为了这个目标,我创建了一个类来设置和读取值.但问题是每当我调用appIsPurchased()方法时,结果都是真的,因为自应用程序安装和第一次初始启动以来它没有被更改.

这是我的代码:

/**
 * 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;
    }

} …
Run Code Online (Sandbox Code Playgroud)

android sharedpreferences

7
推荐指数
2
解决办法
2万
查看次数

共享首选项的android默认值

我想了解Android的SharedPreferences.我是初学者,不太了解它.

我有我为我的应用程序首选项实现的这个类

public class Preferences {
    public static final String MY_PREF = "MyPreferences";

    private SharedPreferences sharedPreferences;
    private Editor editor;

    public Preferences(Context context) {
        this.sharedPreferences = context.getSharedPreferences(MY_PREF, 0);
        this.editor = this.sharedPreferences.edit();
    }

    public void set(String key, String value) {
        this.editor.putString(key, value);
        this.editor.commit();
    }

    public String get(String key) {
        return this.sharedPreferences.getString(key, null);
    }

    public void clear(String key) {
        this.editor.remove(key);
        this.editor.commit();
    }

    public void clear() {
        this.editor.clear();
        this.editor.commit();
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是我想设置默认首选项.它们将在安装应用程序时设置,并且可以在应用程序之后进行修改并保持持久性.我听说过一个preferences.xml,但我不明白这个过程.

有人能帮助我吗?

谢谢你的时间

android default sharedpreferences

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×2

sharedpreferences ×2

default ×1