Android之SharedPreferences工具类

来源:互联网 发布:qq三国桃园第一js蘑菇 编辑:程序博客网 时间:2024/06/05 15:20

本工具类永久维护,永久更新,如果各位读者发现有bug或者不合理之处,请留言,博主将第一时间改正。


本工具类提供的功能有:

1.存储五种类型的数据;

2.读取五种类型的数据;


内容如下:

import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;/** *  * @author bear *  *         SharedPrefereces工具类 *  */public class SharedPrefsUtil {/** * 向SharedPreferences中写入int类型数据 *  * @param context 上下文环境 * @param name 对应的xml文件名称 * @param key 键 * @param value 值 */public static void putValue(Context context, String name, String key,int value) {Editor sp = getEditor(context, name);sp.putInt(key, value);sp.commit();}/** * 向SharedPreferences中写入boolean类型的数据 *  * @param context 上下文环境 * @param name 对应的xml文件名称 * @param key 键 * @param value 值 */public static void putValue(Context context, String name, String key,boolean value) {Editor sp = getEditor(context, name);sp.putBoolean(key, value);sp.commit();}/** * 向SharedPreferences中写入String类型的数据 *  * @param context 上下文环境 * @param name 对应的xml文件名称 * @param key 键 * @param value 值 */public static void putValue(Context context, String name, String key,String value) {Editor sp = getEditor(context, name);sp.putString(key, value);sp.commit();}/** * 向SharedPreferences中写入float类型的数据 *  * @param context 上下文环境 * @param name 对应的xml文件名称 * @param key 键 * @param value 值 */public static void putValue(Context context, String name, String key,float value) {Editor sp = getEditor(context, name);sp.putFloat(key, value);sp.commit();}/** * 向SharedPreferences中写入long类型的数据 *  * @param context 上下文环境 * @param name 对应的xml文件名称 * @param key 键 * @param value 值 */public static void putValue(Context context, String name, String key,long value) {Editor sp = getEditor(context, name);sp.putLong(key, value);sp.commit();}/** * 从SharedPreferences中读取int类型的数据 *  * @param context 上下文环境 * @param name 对应的xml文件名称 * @param key 键 * @param defValue 如果读取不成功则使用默认值 * @return 返回读取的值 */public static int getValue(Context context, String name, String key,int defValue) {SharedPreferences sp = getSharedPreferences(context, name);int value = sp.getInt(key, defValue);return value;}/** * 从SharedPreferences中读取boolean类型的数据 *  * @param context 上下文环境 * @param name 对应的xml文件名称 * @param key 键 * @param defValue 如果读取不成功则使用默认值 * @return 返回读取的值 */public static boolean getValue(Context context, String name, String key,boolean defValue) {SharedPreferences sp = getSharedPreferences(context, name);boolean value = sp.getBoolean(key, defValue);return value;}/** * 从SharedPreferences中读取String类型的数据 *  * @param context 上下文环境 * @param name 对应的xml文件名称 * @param key 键 * @param defValue 如果读取不成功则使用默认值 * @return 返回读取的值 */public static String getValue(Context context, String name, String key,String defValue) {SharedPreferences sp = getSharedPreferences(context, name);String value = sp.getString(key, defValue);return value;}/** * 从SharedPreferences中读取float类型的数据 *  * @param context 上下文环境 * @param name 对应的xml文件名称 * @param key 键 * @param defValue 如果读取不成功则使用默认值 * @return 返回读取的值 */public static float getValue(Context context, String name, String key,float defValue) {SharedPreferences sp = getSharedPreferences(context, name);float value = sp.getFloat(key, defValue);return value;}/** * 从SharedPreferences中读取long类型的数据 *  * @param context 上下文环境 * @param name 对应的xml文件名称 * @param key 键 * @param defValue 如果读取不成功则使用默认值 * @return 返回读取的值 */public static long getValue(Context context, String name, String key,long defValue) {SharedPreferences sp = getSharedPreferences(context, name);long value = sp.getLong(key, defValue);return value;}//获取Editor实例private static Editor getEditor(Context context, String name) {return getSharedPreferences(context, name).edit();}//获取SharedPreferences实例private static SharedPreferences getSharedPreferences(Context context, String name) {return context.getSharedPreferences(name, Context.MODE_PRIVATE);}}

如果你的工程支持版本信息Android API Level >= 11,那么你还可以加入Set<String>类型的存储和读取。


在Activity或者其他的地方调用方式举例如下:

//存储数据,在MySetting文件中存储键值对("color","red")SharedPrefsUtil.putValue(this, "MySetting", "color", "red");
//读取数据,从MySetting文件中读取键为"color"的值String color = SharedPrefsUtil.getValue(this, "MySetting", "color", "blue");


2 0