我写了一篇小文章,也可以在这里找到。它描述了什么是:SharedPreferences
最佳做法:共享首选项
Android 提供了许多存储应用程序数据的方法。其中一种方式将我们引向共享首选项对象,该对象用于以键值对的形式存储私有基元数据。
所有逻辑仅基于三个简单类:
共享首选项
SharedPreferences
是其中的主要。它负责获取(解析)存储的数据,提供用于获取对象的接口以及用于添加和删除的接口Editor
OnSharedPreferenceChangeListener
- 要创建,您将需要对象(可以是应用程序)
SharedPreferences
Context
Context
)
-
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() 之前不会复制回原始更改SharedPreferences
SharedPreferences
- 使用简单的界面将值放入
Editor
- 保存与速度更快或异步的值。其实用不同的线程使用更安全。这就是为什么我更喜欢使用 commit
()
。commit()
apply
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
-
与单例对象一样,您可以更改任何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
请记住,首选项对象越大,、、和操作就越长。因此,强烈建议将数据分隔在不同的小对象中。get
commit
apply
remove
clear
-
应用程序更新后,您的首选项将不会被删除。因此,在某些情况下,您需要创建一些迁移方案。例如,您有在应用程序启动中解析本地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();
}
}