Android SharedPreferences最佳实践

Kam*_*eld 55 java android

在我构建的应用程序中,我们依赖于SharedPreferences,这让我想到了访问SharedPreferences时的最佳实践.例如,许多人说通过此调用访问它的适当方式:

PreferenceManager.getDefaultSharedPreferences(Context context)
Run Code Online (Sandbox Code Playgroud)

然而,似乎这可能是危险的.如果您有一个依赖于SharedPreferences的大型应用程序,则可能存在密钥重复,尤其是在使用依赖于SharedPreferences的某些第三方库的情况下.在我看来,更好的使用呼叫将是:

Context.getSharedPreferences(String name, int mode)
Run Code Online (Sandbox Code Playgroud)

这样,如果您有一个严重依赖于SharedPreferences的类,则可以创建仅由您的类使用的首选项文件.您可以使用类的完全限定名称来确保该文件很可能不会被其他人复制.

同样基于这个SO问题:是否应该通过UI线程访问SharedPreferences?,似乎访问SharedPreferences应该在UI线程完成,这是有道理的.

在他们的应用程序中使用SharedPreferences时,Android开发人员应该注意哪些其他最佳实践?

Yak*_*pan 88

我写了一篇文章也可以在这里找到.它描述了什么SharedPreferences:

最佳实践: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,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)

  • @Semanticer首选项是一个文件,大到了 - 执行所有文件操作所需的时间越长.单独的首选项文件.不要将所有数据保存在一个`context.getSharedPreferences("com.example.app",Context.MODE_PRIVATE);`中.实际上你可以尝试和你的情况进行比较.也许一个文件不会影响您的性能.但是,在小的可理解的和平中分离逻辑总是更好 (3认同)

Com*_*are 39

如果您有一个依赖于SharedPreferences的大型应用程序,则可能存在密钥重复,尤其是在使用依赖于SharedPreferences的某些第三方库的情况下.

图书馆不应该使用那个特定的SharedPreferences.默认值SharedPreferences应仅由应用程序使用.

这样,如果您有一个严重依赖于SharedPreferences的类,则可以创建仅由您的类使用的首选项文件.

当然欢迎你这样做.在应用程序级别,我不会将SharedPreferences它们作为应用程序中的组件共享的主要原因.开发团队应该没有问题来管理这个命名空间,就像管理类,包,资源或其他项目级别的东西时应该没有问题.此外,默认值SharedPreferences是您PreferenceActivity将使用的.

但是,回到您的库点,可重用的库应该SharedPreferences只为它们的库使用单独的库.我不会将它建立在一个类名上,因为那样你就可以重新打破你的应用了.相反,选择一个唯一的名称(例如,基于库名称,例如"com.commonsware.cwac.wakeful.WakefulIntentService")但稳定.

似乎访问SharedPreferences应该在UI线程之外完成,这是有意义的.

理想情况下,是的.我最近发布了一个SharedPreferencesLoader有助于此的内容.

在他们的应用程序中使用SharedPreferences时,Android开发人员应该注意哪些其他最佳实践?

不要过分依赖它们.它们存储在XML文件中,不是事务性的.数据库应该是您的主要数据存储,特别是对于您不想丢失的数据.

  • @root:`SharedPreferences`被缓存.第一次访问特定的`SharedPreferences`加载XML; 后续读取将在缓存中起作用. (2认同)

Sag*_*ain 6

在 kotlin 中,SharedPreferences可以通过以下方式简化使用。

class Prefs(context: Context) {

    companion object {
        private const val PREFS_FILENAME = "app_prefs"

        private const val KEY_MY_STRING = "my_string"
        private const val KEY_MY_BOOLEAN = "my_boolean"
        private const val KEY_MY_ARRAY = "string_array"
    }

    private val sharedPrefs: SharedPreferences =
        context.getSharedPreferences(PREFS_FILENAME, Context.MODE_PRIVATE)

    var myString: String
        get() = sharedPrefs.getString(KEY_MY_STRING, "") ?: ""
        set(value) = sharedPrefs.edit { putString(KEY_MY_STRING, value) }

    var myBoolean: Boolean
        get() = sharedPrefs.getBoolean(KEY_MY_BOOLEAN, false)
        set(value) = sharedPrefs.edit { putBoolean(KEY_MY_BOOLEAN, value) }

    var myStringArray: Array<String>
        get() = sharedPrefs.getStringSet(KEY_MY_ARRAY, emptySet())?.toTypedArray()
            ?: emptyArray()
        set(value) = sharedPrefs.edit { putStringSet(KEY_MY_ARRAY, value.toSet()) }
Run Code Online (Sandbox Code Playgroud)

这里,sharedPrefs.edit{...}由android核心ktx库提供,应该通过implementation "androidx.core:core-ktx:1.0.2"在应用层添加依赖来实现build.gradle

您可以SharedPreferences使用代码获取实例:

val prefs = Prefs(context)
Run Code Online (Sandbox Code Playgroud)

此外,您可以在应用程序内的任何位置创建和使用的Singleton对象Prefs

val prefs: Prefs by lazy {
    Prefs(App.instance)
}
Run Code Online (Sandbox Code Playgroud)

其中,App扩展Application并应包含在AndroidManifest.xml

应用程序

class App:Application() {
    companion object {
        lateinit var instance: App
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
    }
}
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml

class Prefs(context: Context) {

    companion object {
        private const val PREFS_FILENAME = "app_prefs"

        private const val KEY_MY_STRING = "my_string"
        private const val KEY_MY_BOOLEAN = "my_boolean"
        private const val KEY_MY_ARRAY = "string_array"
    }

    private val sharedPrefs: SharedPreferences =
        context.getSharedPreferences(PREFS_FILENAME, Context.MODE_PRIVATE)

    var myString: String
        get() = sharedPrefs.getString(KEY_MY_STRING, "") ?: ""
        set(value) = sharedPrefs.edit { putString(KEY_MY_STRING, value) }

    var myBoolean: Boolean
        get() = sharedPrefs.getBoolean(KEY_MY_BOOLEAN, false)
        set(value) = sharedPrefs.edit { putBoolean(KEY_MY_BOOLEAN, value) }

    var myStringArray: Array<String>
        get() = sharedPrefs.getStringSet(KEY_MY_ARRAY, emptySet())?.toTypedArray()
            ?: emptyArray()
        set(value) = sharedPrefs.edit { putStringSet(KEY_MY_ARRAY, value.toSet()) }
Run Code Online (Sandbox Code Playgroud)

示例用法:

// get stored value
val myString = prefs.myString

// store value
prefs.myString = "My String Value"

// get stored array
val myStringArray = prefs.myStringArray

// store array
prefs.myStringArray = arrayOf("String 1","String 2","String 3")
Run Code Online (Sandbox Code Playgroud)


Luk*_*kap 5

这是我的方式

用于写

SharedPreferences settings = context.getSharedPreferences("prefs", 0);
SharedPreferences.Editor editore = settings.edit();
editore.putString("key", "some value");
editore.apply();
Run Code Online (Sandbox Code Playgroud)

读书

SharedPreferences settings = getSharedPreferences("prefs", 0);
Strings value = settings.getString("key", "");
Run Code Online (Sandbox Code Playgroud)