SharedPreferences的最佳实践

Yak*_*pan 11 android model android-preferences sharedpreferences

关于SharedPreferences我正在寻找的一系列问题:

  • 什么,为什么,什么时候?
  • 它是如何在里面工作的?
  • 使用它的最佳实践?

这里只回答了其中一些问题.这就是我进行调查和测试的原因.

当我回答我自己的问题时,我决定与其他人分享答案.

Yak*_*pan 24

我写了一篇文章也可以在这里找到.

最佳实践:SharedPreferences

Android提供了许多存储应用程序数据的方法.其中一种方法将我们引向SharedPreferences对象,该对象用于将私有原始数据存储在键值对中.

所有逻辑仅基于三个简单类:

SharedPreferences

SharedPreferences是他们的主要成员.它负责获取(解析)存储的数据,提供用于获取Editor对象和接口以进行添加和删除的接口OnSharedPreferenceChangeListener

  • 要创建SharedPreferences你将需要Context对象(可以是一个应用程序Context)
  • getSharedPreferences方法解析首选项文件并Map为其创建对象
  • 您可以在Context提供的几种模式下创建它,强烈建议使用MODE_PRIVATE,因为创建世界可读/可写文件非常危险,并可能导致应用程序出现安全漏洞

    // parse Preference file
    SharedPreferences preferences = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
    
    // get values from Map
    preferences.getBoolean("key", defaultValue)
    preferences.get..("key", defaultValue)
    
    // you can get all Map but be careful you must not modify the collection returned by this
    // method, or alter any of its contents.
    Map<String, ?> all = preferences.getAll();
    
    // get Editor object
    SharedPreferences.Editor editor = preferences.edit();
    
    //add on Change Listener
    preferences.registerOnSharedPreferenceChangeListener(mListener);
    
    //remove on Change Listener
    preferences.unregisterOnSharedPreferenceChangeListener(mListener);
    
    // listener example
    SharedPreferences.OnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener
        = new SharedPreferences.OnSharedPreferenceChangeListener() {
      @Override
      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
      }
    };
    
    Run Code Online (Sandbox Code Playgroud)

编辑

SharedPreferences.Editor是用于修改SharedPreferences对象中的值的接口.您在编辑器中进行的所有更改都是批处理的,在SharedPreferences调用commit()或apply()之前不会复制回原始更改

  • 使用简单的界面来输入值 Editor
  • 保存与其同步commit()或异步的值apply更快.实际上使用不同的线程使用commit()更安全.这就是我喜欢使用的原因commit().
  • 使用删除单个值remove()或清除所有值clear()

    // get Editor object
    SharedPreferences.Editor editor = preferences.edit();
    
    // put values in editor
    editor.putBoolean("key", value);
    editor.put..("key", value);
    
    // remove single value by key
    editor.remove("key");
    
    // remove all values
    editor.clear();
    
    // commit your putted values to the SharedPreferences object synchronously
    // returns true if success
    boolean result = editor.commit();
    
    // do the same as commit() but asynchronously (faster but not safely)
    // returns nothing
    editor.apply();
    
    Run Code Online (Sandbox Code Playgroud)

表现和提示

  • SharedPreferences是一个Singleton对象,因此您可以轻松地获得所需数量的引用,它只在您getSharedPreferences第一次调用时打开文件,或者只为它创建一个引用.

    // There are 1000 String values in preferences
    
    SharedPreferences first = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
    // call time = 4 milliseconds
    
    SharedPreferences second = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
    // call time = 0 milliseconds
    
    SharedPreferences third = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
    // call time = 0 milliseconds
    
    Run Code Online (Sandbox Code Playgroud)
  • 由于SharedPreferences单件对象,你可以改变任何它的实例,而不是害怕他们的数据会有所不同

    first.edit().putInt("key",15).commit();
    
    int firstValue = first.getInt("key",0)); // firstValue is 15
    int secondValue = second.getInt("key",0)); // secondValue is also 15
    
    Run Code Online (Sandbox Code Playgroud)
  • 当您get第一次调用方法时,它会按键解析值并将此值添加到地图中.因此,对于第二次调用,它只是从地图获取它,而不进行解析.

    first.getString("key", null)
    // call time = 147 milliseconds
    
    first.getString("key", null)
    // call time = 0 milliseconds
    
    second.getString("key", null)
    // call time = 0 milliseconds
    
    third.getString("key", null)
    // call time = 0 milliseconds
    
    Run Code Online (Sandbox Code Playgroud)
  • 记得优先对象是越长越大get,commit,apply,removeclear操作将是.因此强烈建议将数据分成不同的小对象.

  • 应用程序更新后,您的首选项将不会被删除.因此,有些情况下您需要创建一些迁移方案.例如,你有Application在应用程序启动时解析本地JSON,只有在第一次启动后才决定保存boolean标志wasLocalDataLoaded.一段时间后,您更新了JSON并发布了新的应用程序版本.用户将更新他们的应用程序但他们不会加载新的JSON,因为他们已经在第一个应用程序版本中完成了它.

    public class MigrationManager {
     private final static String KEY_PREFERENCES_VERSION = "key_preferences_version";
     private final static int PREFERENCES_VERSION = 2;
    
     public static void migrate(Context context) {
         SharedPreferences preferences = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
         checkPreferences(preferences);
     }
    
     private static void checkPreferences(SharedPreferences thePreferences) {
         final double oldVersion = thePreferences.getInt(KEY_PREFERENCES_VERSION, 1);
    
         if (oldVersion < PREFERENCES_VERSION) {
             final SharedPreferences.Editor edit = thePreferences.edit();
             edit.clear();
             edit.putInt(KEY_PREFERENCES_VERSION, currentVersion);
             edit.commit();
         }
     }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • SharedPreferences 存储在app数据文件夹中的xml文件中

    // yours preferences
    /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml
    
    // default preferences
    /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml
    
    Run Code Online (Sandbox Code Playgroud)

Android指南.

示例代码

public class PreferencesManager {

    private static final String PREF_NAME = "com.example.app.PREF_NAME";
    private static final String KEY_VALUE = "com.example.app.KEY_VALUE";

    private static PreferencesManager sInstance;
    private final SharedPreferences mPref;

    private PreferencesManager(Context context) {
        mPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    public static synchronized void initializeInstance(Context context) {
        if (sInstance == null) {
            sInstance = new PreferencesManager(context);
        }
    }

    public static synchronized PreferencesManager getInstance() {
        if (sInstance == null) {
            throw new IllegalStateException(PreferencesManager.class.getSimpleName() +
                    " is not initialized, call initializeInstance(..) method first.");
        }
        return sInstance;
    }

    public void setValue(long value) {
        mPref.edit()
                .putLong(KEY_VALUE, value)
                .commit();
    }

    public long getValue() {
        return mPref.getLong(KEY_VALUE, 0);
    }

    public void remove(String key) {
        mPref.edit()
                .remove(key)
                .commit();
    }

    public boolean clear() {
        return mPref.edit()
                .clear()
                .commit();
    }
}
Run Code Online (Sandbox Code Playgroud)