除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本
质是基于XML文件存储key- value键值 对数据,通常用来存储一些简单的配置信
息。其存储位置在/data/data/<包名>/shared_prefs目录下。SharedPreferences
对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。实
现SharedPreferences存储的步骤如下:
一、根据Context获取SharedPreferences对象
二、利用edit()方法获取Editor对象。
三、通过Editor对象存储key-value键值对数据。
四、通过commit()方法提交数据。
下述对Preferences做了一个简单的封装,方便使用
public class PreferencesUtils {
public static String PREFERENCE_NAME ="xml_db";
/**
* put string preferences
*
* @param context
* @param key The name of the preferenceto modify
* @param value The new value for thepreference
* @return True if the new values weresuccessfully written to persistent storage.
*/
public static boolean putString(Contextcontext, String key, String value) { SharedPreferences settings =context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);SharedPreferences.Editor editor = settings.edit(); editor.putString(key, value);return editor.commit(); }
/**
* get string preferences
*
* @param context
* @param key The name of the preferenceto retrieve
* @return The preference value if itexists, or null. Throws ClassCastException if there is a preference with this
* name that is not a string
* @see #getString(Context, String,String) */ public static String getString(Context context, String key) { returngetString(context, key, null); }
/**
* get string preferences
*
* @param context
* @param key The name of the preferenceto retrieve
* @param defaultValue Value to return ifthis preference does not exist
* @return The preference value if itexists, or defValue. Throws ClassCastException if there is a preference with
* this name that is not a string
*/
public static String getString(Contextcontext, String key, String
defaultValue) {
SharedPreferences settings =
context.getSharedPreferences(PREFERENCE_NAME,Context.MODE_PRIVATE); return settings.getString(key, defaultValue); }
/**
* put int preferences
*
* @param context
* @param key The name of the preferenceto modify
* @param value The new value for thepreference
* @return True if the new values weresuccessfully written to persistent storage.
*/
public static boolean putInt(Contextcontext, String key, int value) { SharedPreferences settings =context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);SharedPreferences.Editor editor = settings.edit(); editor.putInt(key, value);return editor.commit(); }
/**
* get int preferences
*
* @param context
* @param key The name of the preferenceto retrieve
* @return The preference value if itexists, or -1. Throws ClassCastException if there is a preference with this
* name that is not a int
* @see #getInt(Context, String, int)
*/
public static int getInt(Context context,String key) { return getInt(context, key, -1); }
/**
* get int preferences
*
* @param context
* @param key The name of the preferenceto retrieve
* @param defaultValue Value to return ifthis preference does not exist
* @return The preference value if itexists, or defValue. Throws ClassCastException if there is a preference with
* this name that is not a int
*/
public static int getInt(Contextcontext, String key, int defaultValue) { SharedPreferences settings =context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); returnsettings.getInt(key, defaultValue); }
/**
* put long preferences
*
* @param context
* @param key The name of the preferenceto modify
* @param value The new value for thepreference
* @return True if the new values weresuccessfully written to persistent storage.
*/
public static boolean putLong(Contextcontext, String key, long value) { SharedPreferences settings =context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editoreditor = settings.edit(); editor.putLong(key, value); return editor.commit(); }
/**
* get long preferences
*
* @param context
* @param key The name of the preferenceto retrieve
* @return The preference value if itexists, or -1. Throws ClassCastException if there is a preference with this
* name that is not a long
* @see #getLong(Context, String, long)
*/
public static long getLong(Contextcontext, String key) { return getLong(context, key, -1); }
/**
* get long preferences
*
* @param context
* @param key The name of the preferenceto retrieve
* @param defaultValue Value to return ifthis preference does not exist
* @return The preference value if itexists, or defValue. Throws ClassCastException if there is a preference with
* this name that is not a long
*/
public static long getLong(Contextcontext, String key, long defaultValue) { SharedPreferences settings =context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); returnsettings.getLong(key, defaultValue); }
/**
* put float preferences
*
* @param context
* @param key The name of the preferenceto modify
* @param value The new value for thepreference
* @return True if the new values weresuccessfully written to persistent storage.
*/
public static boolean putFloat(Contextcontext, String key, float value) { SharedPreferences settings =context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);SharedPreferences.Editor editor = settings.edit(); editor.putFloat(key, value);return editor.commit(); }
/**
* get float preferences
*
* @param context
* @param key The name of the preferenceto retrieve
* @return The preference value if itexists, or -1. Throws ClassCastException if there is a preference with this
* name that is not a float
* @see #getFloat(Context, String, float)*/ public static float getFloat(Context context, String key) { returngetFloat(context, key, -1); }
/**
* get float preferences
*
* @param context
* @param key The name of the preferenceto retrieve
* @param defaultValue Value to return ifthis preference does not exist
* @return The preference value if itexists, or defValue. Throws ClassCastException if there is a preference with
* this name that is not a float
*/
public static float getFloat(Contextcontext, String key, float
defaultValue) {
SharedPreferences settings =
context.getSharedPreferences(PREFERENCE_NAME,Context.MODE_PRIVATE); return settings.getFloat(key, defaultValue); }
/**
* put boolean preferences
*
* @param context
* @param key The name of the preferenceto modify
* @param value The new value for thepreference
* @return True if the new values weresuccessfully written to persistent storage.
*/
public static boolean putBoolean(Contextcontext, String key, boolean
value) {
SharedPreferences settings =
context.getSharedPreferences(PREFERENCE_NAME,Context.MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit();editor.putBoolean(key, value); return editor.commit(); }
/**
* get boolean preferences, default isfalse
*
* @param context
* @param key The name of the preferenceto retrieve
* @return The preference value if itexists, or false. Throws ClassCastException if there is a preference with this
* name that is not a boolean
* @see #getBoolean(Context, String,boolean) */ public static boolean getBoolean(Context context, String key) {return getBoolean(context, key, false); }
/**
* get boolean preferences
*
* @param context
* @param key The name of the preferenceto retrieve
* @param defaultValue Value to return ifthis preference does not exist
* @return The preference value if itexists, or defValue. Throws ClassCastException if there is a preference with
* this name that is not a boolean
*/
public static boolean getBoolean(Contextcontext, String key, boolean
defaultValue) {
SharedPreferences settings =
context.getSharedPreferences(PREFERENCE_NAME,Context.MODE_PRIVATE); return settings.getBoolean(key, defaultValue); } }