繁体   English   中英

自定义 SharedPreferences 类

[英]Custom SharedPreferences class

我是 android 和学习的新手。 我的应用程序中有主题更改选项,用户可以从中切换主题。 我正在使用一个全局变量来保存应用程序中的主题编号,但是当应用程序从后台清除时它会丢失。 所以我想为此目的使用 SharedPreferences。 我找到了一种从这里存储和检索 SharedPreference 的简单易行的方法。

我的代码如下

public class Keystore {
    private static Keystore store;
    private SharedPreferences SP;
    private static String filename="Keys";
    public static int theme=1;

    private Keystore(Context context) {
        SP = context.getApplicationContext().getSharedPreferences(filename,0);
    }

    public static Keystore getInstance(Context context) {
        if (store == null) {
            store = new Keystore(context);
        }
        return store;
    }

    public void put(String key, String value) {
        SharedPreferences.Editor editor;
        editor = SP.edit();
        editor.putString(key, value);
        editor.apply();
    }

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

    public int getInt(String key) {
        return SP.getInt(key, 0);
    }

    public void putInt(String key, int num) {
        SharedPreferences.Editor editor;
        editor = SP.edit();

        editor.putInt(key, num);
        editor.apply();
    }

    public void clear(){
        SharedPreferences.Editor editor;
        editor = SP.edit();

        editor.clear();
        editor.apply();
    }

    public void remove(){
        SharedPreferences.Editor editor;
        editor = SP.edit();

        editor.remove(filename);
        editor.apply();
    }
}

根据原始答案中给出的示例,我试图在我的活动类中使用它,如下所示以获得价值

int theme= store.getInt("theme");
Log.d(getClass().getName(),"theme"+theme);

但它返回 0 而不是 1。我也怀疑我是否在该类中将默认值保存为 1,例如public static int theme=1; 这是在 SharedPreferences 中保存默认值的正确方法吗?

谢谢

你应该使用commit ()

将此编辑器的首选项更改提交回它正在编辑的 SharedPreferences 对象。 这会自动执行请求的修改,替换当前 SharedPreferences 中的任何内容。

public void putInt(String key, int num) 
    {
        SharedPreferences.Editor editor;
        editor = SP.edit();

        editor.remove("key");
        editor.putString("key", num);
        editor.commit(); // IF commit() showing warning then use apply() instead .
        editor.apply();

    }

笔记

如果您不关心返回值并且正在从应用程序的主线程使用它,请考虑改用 apply()。

如果您想在不将数据保存在 sharepreferance 中时默认获取 1 个值,那么您必须在您的方法中进行更改,例如

public int getInt(String key) {
    return SP.getInt(key, 1);
}

它会为你工作。

您必须先在共享首选项中保存值,以便以后可以检索它。 要保存它,请使用以下代码

store.putInt(your int value);

并像您一样从共享偏好中检索它

int theme= store.getInt("theme");

你可以这样做:

private void saveTheme()
{
SharedPreferences sharedpreferences = getSharedPreferences("filename here", Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putInt("theme", 1);
editor.commit();
} 



private int getTheme()
{
SharedPreferences sharedpreferences = getSharedPreferences("filename here", Context.MODE_PRIVATE);
sharedpreferences.getInt("theme",0);
}

您可以将它们添加到 Utility 类中或创建一个单独的 PreferenceHelper 类并使 sharedPreference 全局化。

希望能帮助到你 !!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM