【问题标题】:Custom SharedPreferences class自定义 SharedPreferences 类
【发布时间】:2017-10-16 15:10:24
【问题描述】:

我是 android 和学习的新手。我的应用程序中有主题更改选项,用户可以从中切换主题。我使用全局变量在应用程序中保存主题编号,但是当应用程序从后台清除时它会丢失。所以我考虑过为此目的使用 SharedPreferences 。我找到了一种从here 存储和检索 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。我也怀疑我在 public static int theme=1; 这样的类中将默认值保存为 1@ 这是在 SharedPreferences 中保存默认值的正确方法吗?

谢谢

【问题讨论】:

  • 您在共享偏好中保存的价值在哪里?
  • @VivekMishra 我用它像 public static int theme=1;默认情况下,直到现在还没有提交任何保存。
  • 如果您没有保存任何值,那么您从共享偏好中获取什么?它将始终返回整数的默认值,即 0

标签: java android sharedpreferences


【解决方案1】:

你应该使用commit ()

将您的首选项更改从该编辑器提交回 它正在编辑的 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()。

【讨论】:

  • 现在提交使用来发出警告。建议使用 apply 代替
  • @VivekMishra 确实。
【解决方案2】:

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

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

它会为你工作。

【讨论】:

  • 嗨!谢谢你的好建议。我还有一个问题。在 Example 类中,它使用 SP = context.getApplicationContext().getSharedPreferences(filename,0);像这样。如果我想存储多个密钥怎么办
  • 欲了解更多有关共享偏好的信息,请参阅链接:journaldev.com/9412/android-shared-preferences-example-tutorial
  • 如果您喜欢我的回答,请接受并点赞。 :)
【解决方案3】:

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

store.putInt(your int value);

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

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

【讨论】:

    【解决方案4】:

    你可以这样做:

    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 全局化。

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      • 2012-09-04
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      相关资源
      最近更新 更多