简体   繁体   English

自定义 SharedPreferences 类

[英]Custom SharedPreferences class

I am new to android and learning.我是 android 和学习的新手。 I have theme changing option in my application from where the user can switch themes.我的应用程序中有主题更改选项,用户可以从中切换主题。 I was using a global variable for saving theme number in the app but it was getting a loss when application get cleared from the background.我正在使用一个全局变量来保存应用程序中的主题编号,但是当应用程序从后台清除时它会丢失。 So I have thought for use SharedPreferences for this purpose.所以我想为此目的使用 SharedPreferences。 I have found one simple and easy way to store and retrieve SharedPreference from here .我找到了一种从这里存储和检索 SharedPreference 的简单易行的方法。

My code is like below :我的代码如下

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();
    }
}

And as per example given in original answer, I am trying to use it in my activity class like below for getting value根据原始答案中给出的示例,我试图在我的活动类中使用它,如下所示以获得价值

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

But it's returning 0 instead of 1. I have also doubt that I have saved default value as 1 in that class like public static int theme=1;但它返回 0 而不是 1。我也怀疑我是否在该类中将默认值保存为 1,例如public static int theme=1; This is the correct way for saving default value in SharedPreferences?这是在 SharedPreferences 中保存默认值的正确方法吗?

Thanks谢谢

You should use commit ()你应该使用commit ()

Commit your preferences changes back from this Editor to the SharedPreferences object it is editing.将此编辑器的首选项更改提交回它正在编辑的 SharedPreferences 对象。 This atomically performs the requested modifications, replacing whatever is currently in the 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();

    }

NOTE笔记

If you don't care about the return value and you're using this from your application's main thread, consider using apply() instead.如果您不关心返回值并且正在从应用程序的主线程使用它,请考虑改用 apply()。

If you want to fetch by default 1 value when you when you don't save data in sharepreferance then you have to change in in your method like如果您想在不将数据保存在 sharepreferance 中时默认获取 1 个值,那么您必须在您的方法中进行更改,例如

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

It will work for you.它会为你工作。

You have to first save value in shared preferences so that you can retrieve it later.您必须先在共享首选项中保存值,以便以后可以检索它。 To save it use the below code要保存它,请使用以下代码

store.putInt(your int value);

and retrieve it from shared preference same like you are doing并像您一样从共享偏好中检索它

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

You can do it this way :你可以这样做:

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);
}

You can add them in Utility class or make a seperate PreferenceHelper class and make sharedPreference global.您可以将它们添加到 Utility 类中或创建一个单独的 PreferenceHelper 类并使 sharedPreference 全局化。

Hope it Helps !!希望能帮助到你 !!

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

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