安卓共享偏好设置最佳实践

2022-08-31 14:28:44

在我一直在构建的应用程序中,我们非常依赖共享首选项,这让我思考在访问共享首选项时什么是最佳实践。例如,许多人说访问它的适当方式是通过以下调用:

PreferenceManager.getDefaultSharedPreferences(Context context)

然而,这似乎可能是危险的。如果您有一个依赖于共享首选项的大型应用程序,则可能会有密钥复制,尤其是在使用某些也依赖于共享首选项的第三方库的情况下。在我看来,更好的调用是:

Context.getSharedPreferences(String name, int mode)

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

同样基于这个SO问题:访问共享首选项是否应该在UI线程之外完成?,似乎访问共享首选项应该在UI线程之外完成,这是有道理的。

Android 开发人员在应用程序中使用共享首选项时是否应注意任何其他最佳实践?


答案 1

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

最佳做法:共享首选项

Android 提供了许多存储应用程序数据的方法。其中一种方式将我们引向共享首选项对象,该对象用于以键值对的形式存储私有基元数据。

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

共享首选项

SharedPreferences是其中的主要。它负责获取(解析)存储的数据,提供用于获取对象的接口以及用于添加和删除的接口EditorOnSharedPreferenceChangeListener

  • 要创建,您将需要对象(可以是应用程序)SharedPreferencesContextContext)
  • getSharedPreferences方法解析首选项文件并为其创建对象Map
  • 您可以在 Context 提供的几个模式下创建它。应始终使用MODE_PRIVATE,因为自 API 级别 17 以来,所有其他模式都已弃用。

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

编辑 器

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

  • 使用简单的界面将值放入Editor
  • 保存与速度更快或异步的值。其实用不同的线程使用更安全。这就是为什么我更喜欢使用 commit()commit()applycommit()
  • 删除具有单个值的单个值或清除所有值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();
    

性能和提示

  • 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
    
  • 单例对象一样,您可以更改任何It实例,而不必担心它们的数据会有所不同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
    
  • 请记住,首选项对象越大,、、和操作就越长。因此,强烈建议将数据分隔在不同的小对象中。getcommitapplyremoveclear

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

    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();
         }
     }
    }
    
  • SharedPreferences存储在应用数据文件夹的 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
    

安卓指南。

示例代码

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

答案 2

如果您有一个依赖于共享首选项的大型应用程序,则可能会有密钥复制,尤其是在使用某些也依赖于共享首选项的第三方库的情况下。

库不应使用该特定 .默认值应仅由应用程序使用。SharedPreferencesSharedPreferences

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

当然欢迎您这样做。在应用程序级别,我不会这样做,因为主要原因是在应用程序中的组件之间共享它们。开发团队管理此命名空间应该没有问题,就像他们应该在管理类、包、资源或其他项目级内容的名称时没有问题一样。此外,默认值是您将要使用的内容。SharedPreferencesSharedPreferencesPreferenceActivity

但是,回到您的库点,可重用库应仅对其库使用单独的库。我不会以类名为基础,因为这样你就是一个重构者,远离破坏你的应用程序。相反,选择一个唯一的名称(例如,基于库名称,例如)但稳定。SharedPreferences"com.commonsware.cwac.wakeful.WakefulIntentService"

似乎访问共享首选项应该在UI线程之外完成,这是有道理的。

理想情况下,是的。我最近发布了一个共享首选项加载器,可以帮助解决这个问题。

Android 开发人员在应用程序中使用共享首选项时是否应注意任何其他最佳实践?

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


推荐