SharedPreferences最佳实践

转:http://blog.csdn.net/xushuaic/article/details/24513599

 

笔记摘要:该文章是我在Android Weekly中看到的,以前也一直用SharedPreferences,不过一直只是会用,并没有深入研究下,既然看过了这篇文章,就翻下记录下来对自己理解也会有帮助,和朋友们分享下。另外提一下Android Weekly,这是一个每周发布一次Android行业最新动态的国外网站,其中的LatestIssue和ToolBox模块你可以关注下,LatestIssue会发布一下比较优秀的新的开源项目,每周我都会看一下,之后一定就在gitHub上找到并start上了。ToolBox模块就是一些好的开源项目了,不过更新的不怎么频繁,大家也可以看下。

先贴上作者发布到GitHub的源代码:Best Practice. SharedPreferences.
原文地址:Best Practice ,以下是译文。

Android开发者有很多方式都可以存储应用的数据。其中之一是SharedPreference对象,该对象可以通过key-value的形式保存私有的数据。

所有的逻辑依赖下面3个类
SharedPreferences
SharedPreferences.Editor
SharedPreferences.OnSharedPreferenceChangeListener

SharedPreferences
SharedPreferences在这3个类中是最主要的。它负提供了获取Editor对象的接口和添加删除的监听:OnSharedPreferenceChangeListener

- 创建SharedPreferences 时,你需要Context对象
- getSharedPreferences方法用于解析Preference文件并为其创建Map对象。
- 你可以通过提供的Context创建不同的模式,强烈建议使用MODE_PRIVATE,因为创建任意可读取的文件是非常危险的,可能会在应用中导致一些安全问题。

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. // parse Preference file  
  2. SharedPreferences preferences = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);  
  3.    
  4. // get values from Map  
  5. preferences.getBoolean("key", defaultValue)  
  6. preferences.get..("key", defaultValue)  
  7.    
  8. // you can get all Map but be careful you must not modify the collection returned by this  
  9. // method, or alter any of its contents.  
  10. Map<String, ?> all = preferences.getAll();  
  11.    
  12. // get Editor object  
  13. SharedPreferences.Editor editor = preferences.edit();  
  14.    
  15. //add on Change Listener  
  16. preferences.registerOnSharedPreferenceChangeListener(mListener);  
  17.    
  18. //remove on Change Listener  
  19. preferences.unregisterOnSharedPreferenceChangeListener(mListener);  
  20.    
  21. // listener example  
  22. SharedPreferences.OnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener  
  23.         = new SharedPreferences.OnSharedPreferenceChangeListener() {  
  24.     @Override  
  25.     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {  
  26.     }  
  27. };  

 

 

Editor
SharedPreference.Editor 是一个用于修改SharedPreferences值的接口。在Editor里所做的所有变更都会被批处理,但是不会立即覆盖到原始的SharedPreferences中,直到你调用了commit()或者apply()。

- 使用简单的接口保存值到Editor中
- 使用commit()同步或者apply()异步保存值(异步更快)。事实上,在不同的线程中使用commit()更安全,这也正是我倾向于使用commit()的原因。
- 通过remove()移除单一的值或者通过clear删除所有的值。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. // get Editor object  
  2. SharedPreferences.Editor editor = preferences.edit();  
  3.    
  4. // put values in editor  
  5. editor.putBoolean("key", value);  
  6. editor.put..("key", value);  
  7.    
  8. // remove single value by key  
  9. editor.remove("key");  
  10.    
  11. // remove all values  
  12. editor.clear();  
  13.    
  14. // commit your putted values to the SharedPreferences object synchronously  
  15. // returns true if success  
  16. boolean result = editor.commit();  
  17.    
  18. // do the same as commit() but asynchronously (faster but not safely)  
  19. // returns nothing  
  20. editor.apply();  

 

 

性能和贴士
SharedPreferences 是一个单例对象,因此,你可以在很多地方引用它,获取也很容易。它只会在第一次调用getSharedPreferences的时候打开或者为其创建一个引用。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. // There are 1000 String values in preferences   
  2. SharedPreferences first = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);  
  3. // call time = 4 milliseconds  
  4.    
  5. SharedPreferences second = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);  
  6. // call time = 0 milliseconds  
  7.    
  8. SharedPreferences third = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);  
  9. // call time = 0 milliseconds  

由于SharedPreferences是一个单例对象,你不用担心任意的变更会导致数据的不一致。

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. first.edit().putInt("key",15).commit();  
  2. int firstValue = first.getInt("key",0)); // firstValue is 15  
  3. int secondValue = second.getInt("key",0)); // secondValue is also 15  

 

当你第一次调用get方法的时候,它会通过key解析值,并把这些值添加到map集合中。因此对于下面的second调用它,只会从map中获取,而不再解析。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. first.getString("key", null)  
  2. // call time = 147 milliseconds  
  3.    
  4. first.getString("key", null)  
  5. // call time = 0 milliseconds  
  6.    
  7. second.getString("key", null)  
  8. // call time = 0 milliseconds  
  9.    
  10. third.getString("key", null)  
  11. // call time = 0 milliseconds  

记住Preference对象越大,对于get,commit,apply和clear的操作时间将会越长。因此强烈建议在不同的小对象中分开操作你的数据。
你的preference在应用更新的时候不会被移除。因此,这时候,你需要创建一些新的升级规则。比如你有一个应用,它会在启动的时候解析本地的JSON数据,在你第一次启动后,你决定保存wasLocalDataLoaded 标记。在之后的多次更新JSON数据和发布新版本的时候,用户只会更新它们的应用而不会再加载新的JSON数据,因为它们已经在第一个版本中加载过了。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public class MigrationManager {  
  2.     private final static String KEY_PREFERENCES_VERSION = "key_preferences_version";  
  3.     private final static int PREFERENCES_VERSION = 2;  
  4.    
  5.     public static void migrate(Context context) {  
  6.         SharedPreferences preferences = context.getSharedPreferences("pref", Context.MODE_PRIVATE);  
  7.         checkPreferences(preferences);  
  8.     }  
  9.    
  10.     private static void checkPreferences(SharedPreferences thePreferences) {  
  11.         final double oldVersion = thePreferences.getInt(KEY_PREFERENCES_VERSION, 1);  
  12.    
  13.         if (oldVersion < PREFERENCES_VERSION) {  
  14.             final SharedPreferences.Editor edit = thePreferences.edit();  
  15.             edit.clear();  
  16.             edit.putInt(KEY_PREFERENCES_VERSION, currentVersion);  
  17.             edit.commit();  
  18.         }  
  19.     }  
  20. }  

SharedPreferences 是以XML的格式保存在你的data文件夹中的

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. // yours preferences  
  2. /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml  
  3.    
  4. // default preferences  
  5. /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml  


示例代码

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
    1. public class PreferencesManager {  
    2.    
    3.     private static final String PREF_NAME = "com.example.app.PREF_NAME";  
    4.     private static final String KEY_VALUE = "com.example.app.KEY_VALUE";  
    5.    
    6.     private static PreferencesManager sInstance;  
    7.     private final SharedPreferences mPref;  
    8.    
    9.     private PreferencesManager(Context context) {  
    10.         mPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);  
    11.     }  
    12.    
    13.     public static synchronized void initializeInstance(Context context) {  
    14.         if (sInstance == null) {  
    15.             sInstance = new PreferencesManager(context);  
    16.         }  
    17.     }  
    18.    
    19.     public static synchronized PreferencesManager getInstance() {  
    20.         if (sInstance == null) {  
    21.             throw new IllegalStateException(PreferencesManager.class.getSimpleName() +  
    22.                     " is not initialized, call initializeInstance(..) method first.");  
    23.         }  
    24.         return sInstance;  
    25.     }  
    26.    
    27.     public void setValue(long value) {  
    28.         mPref.edit()  
    29.                 .putLong(KEY_VALUE, value)  
    30.                 .commit();  
    31.     }  
    32.    
    33.     public long getValue() {  
    34.         return mPref.getLong(KEY_VALUE, 0);  
    35.     }  
    36.    
    37.     public void remove(String key) {  
    38.         mPref.edit()  
    39.                 .remove(key)  
    40.                 .commit();  
    41.     }  
    42.    
    43.     public boolean clear() {  
    44.         return mPref.edit()  
    45.                 .clear()  
    46.                 .commit();  
    47.     }  
posted @ 2015-03-25 12:59  狂奔的小狮子  阅读(377)  评论(0编辑  收藏  举报