使用帮助 | 联系电话:400-880-0256 0769-23037585 21686281

Android SharedPreference最佳实践

作者:admin 发表于:2014-08-21 点击:1839  保护视力色:

Android提供多种方式保存应用数据,其中一种方式是SharedPreferences,使用键值对保存私有基本的数据。所有的逻辑仅基于以下三个类:

SharedPreference

SharedPreference在这三个类是最重要的,负责获取(解析)存储数据。提供获取对象的编辑接口,在OnSharedPreferenceChangeListener中提供增加移出对象的接口。

  • 创建SharedPreference对象,需要上下文对象(可以是应用程序的上下文)。
  • getSharedPreferences 方法解析配置文件并创建相关的对象映射。
  • 通过上下文有多种创建它,强烈建议使用MODE_PRIVATE。因为创建一个可读写的文件是非常危险的,容易在应用中产生安全漏洞。
// parse Preference file
SharedPreferences preferences = context.getSharedPreferences("<span class="skimlinks-unlinked">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
        =newSharedPreferences.OnSharedPreferenceChangeListener() {
    @Override
    publicvoidonSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    }
};

Editor

SharedPreferences.Editor是用来修改SharedPreferences对象值的接口。你在editor 做出的修改都是待处理的,并没有被复制到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
booleanresult = editor.commit();
  
// do the same as commit() but asynchronously (faster but not safely)
// returns nothing
editor.apply();

性能和注意事项

SharedPreferences是单例对象,你可以很容易获取你想要的引用。只在你第一次调用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 是单例对象,你可以改变它的实例,不用担心同一个对象数据会不同。

first.edit().putInt(”key”,15).commit();
intfirstValue = first.getInt(”key”,0));// firstValue is 15
intsecondValue = second.getInt(”key”,0));// secondValue is also 15

当你第一次调用get方法时,它解析对象并把放入map中,第二次获取数据从map 中获取,不需再解析。

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方法耗时越长。所以强烈建议把存储的数据分成小的对象。

当你的应用更新以后,你的Preferences不会被移除。所以有些情况下需要创建迁移数据的方案。比如,在应用启动的时候,你的应用解析本地JSON数据,实现这个你需要做的仅仅是存储标志数据(该数据是否为本地数据)。一段时间后,你更新JSON数据发布新的版本,用户会更新应用程序但是不会下载新的JSON数据,因为已经在本地存储了。

publicclassMigrationManager {
    privatefinalstaticString KEY_PREFERENCES_VERSION ="key_preferences_version";
    privatefinalstaticintPREFERENCES_VERSION =2;
 
    publicstaticvoidmigrate(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
        checkPreferences(preferences);
    }
 
    privatestaticvoidcheckPreferences(SharedPreferences thePreferences) {
        finaldoubleoldVersion = thePreferences.getInt(KEY_PREFERENCES_VERSION,1);
 
        if(oldVersion < PREFERENCES_VERSION) {
            finalSharedPreferences.Editor edit = <spanclass="skimlinks-unlinked">thePreferences.edit</span>();
            <spanclass="skimlinks-unlinked">edit.clear</span>();
            edit.putInt(KEY_PREFERENCES_VERSION, currentVersion);
            edit.commit();
        }
    }
}

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

Android指南

示例代码

publicclassPreferencesManager {
 
    privatestaticfinalString PREF_NAME ="<span class="skimlinks-unlinked">com.example.app.PREF_NAME";
    privatestaticfinalString KEY_VALUE ="<span class="skimlinks-unlinked">com.example.app.KEY_VALUE";
 
    privatestaticPreferencesManager sInstance;
    privatefinalSharedPreferences mPref;
 
    privatePreferencesManager(Context context) {
        mPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }
 
    publicstaticsynchronizedvoidinitializeInstance(Context context) {
        if(sInstance ==null) {
            sInstance =newPreferencesManager(context);
        }
    }
 
    publicstaticsynchronizedPreferencesManager getInstance() {
        if(sInstance ==null) {
            thrownewIllegalStateException(PreferencesManager.class.getSimpleName() +
                    " is not initialized, call initializeInstance(..) method first.");
        }
        returnsInstance;
    }
 
    publicvoidsetValue(longvalue) {
        mPref.edit()
                .putLong(KEY_VALUE, value)
                .commit();
    }
 
    publiclonggetValue() {
        returnmPref.getLong(KEY_VALUE,0);
    }
 
    publicvoidremove(String key) {
        mPref.edit()
                .remove(key)
                .commit();
    }
 
    publicbooleanclear() {
        returnmPref.edit()
                .clear()
                .commit();
    }
}

本文代码可以在github上找到。

Android SharedPreference最佳实践,首发于博客 - 伯乐在线