【发布时间】:2014-04-21 15:46:13
【问题描述】:
我正在寻找的关于SharedPreferences 的一组问题:
- 什么、为什么、什么时候?
- 它在内部是如何工作的?
- 使用它的最佳实践?
这里只回答了其中一些问题。这就是我进行一些调查和测试的原因。
因为我已经回答了自己的问题,所以我决定与其他人分享答案。
【问题讨论】:
标签: android model sharedpreferences android-preferences
我正在寻找的关于SharedPreferences 的一组问题:
这里只回答了其中一些问题。这就是我进行一些调查和测试的原因。
因为我已经回答了自己的问题,所以我决定与其他人分享答案。
【问题讨论】:
标签: android model sharedpreferences android-preferences
Yakiv 简洁而优美地提到了有关偏好的所有内容。我只想再补充一点。在共享首选项中编辑数据时,我们通常会这样做
mPref.edit()
每次都会创建一个 SharedPreferences.Editor 类型的新对象,这可能会导致内存中出现不必要的对象。因此您也可以维护对编辑器对象的引用,并节省内存和对象创建时间以及相应的垃圾收集时间。
【讨论】:
我写了一篇小文章,也可以找到here。
Android 提供了多种存储应用程序数据的方法。其中一种方法将我们引向 SharedPreferences 对象,该对象用于将私有原始数据存储在键值对中。
所有逻辑都只基于三个简单的类:
SharedPreferences 是其中的主要部分。它负责获取(解析)存储的数据,提供获取Editor对象的接口和添加和删除OnSharedPreferenceChangeListener的接口
SharedPreferences,您需要Context 对象(可以是应用程序Context)getSharedPreferences 方法解析 Preference 文件并为其创建 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) {
}
};
SharedPreferences.Editor 是一个用于修改SharedPreferences 对象中的值的接口。您在编辑器中所做的所有更改都是批处理的,并且在您调用 commit() 或 apply() 之前不会复制回原始 SharedPreferences
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();
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
由于SharedPreferences 是Singleton 对象,您可以更改任何它的实例,而不必担心它们的数据会有所不同
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
当您第一次调用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
请记住,Preference 对象越大,get、commit、apply、remove 和 clear 操作的时间就越长。因此强烈建议将您的数据分隔在不同的小对象中。
您的偏好不会在应用程序更新后被删除。因此,在某些情况下,您需要创建一些迁移方案。例如,您有在应用程序启动时解析本地 JSON 的应用程序,要在第一次启动后执行此操作,您决定保存布尔标志 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();
}
}
}
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();
}
}
【讨论】: