簡體   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