原创地址:SharedPreferences工具类
我们平常保存一些数据,都会用到SharedPreferences,他是保存在手机里面的,具体路径是data/data/你的包名/shared_prefs/保存的文件名.xml, SharedPreferences的使用也很简单,我自己就写了一个SharedPreferences的工具类,然后就保存在这里,等自己以后需要保存数据直接从这里copy代码,哈哈……
工具类代码如下:
package com.zgs.mobilesafe.utils;
import android.content.Context;
import android.content.SharedPreferences;
/**
* SharedPreferences的一个工具类,包含以下两个函数:
* WriteSpValue保存数据
* ReadSpValue读取数据
* @author zgs
*/
public class SpUtil {
/**
* SharedPreferences的文件名
*/
private static final String FILE_NAME = "share_date";
private static SharedPreferences sp;
/**
* 保存数据
* 根据我们拿到的要保存数据的具体类型,调用不同的保存方法
* @param context 上下文环境
* @param key 存储节点名称
* @param object 存储节点值
*/
public static void WriteSpValue(Context context , String key, Object object){
if (sp==null) {
sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
}
SharedPreferences.Editor editor = sp.edit();
String type = object.getClass().getSimpleName();
if("String".equals(type)){
editor.putString(key, (String)object);
}
else if("Integer".equals(type)){
editor.putInt(key, (Integer)object);
}
else if("Boolean".equals(type)){
editor.putBoolean(key, (Boolean)object);
}
else if("Float".equals(type)){
editor.putFloat(key, (Float)object);
}
else if("Long".equals(type)){
editor.putLong(key, (Long)object);
}
editor.commit();
}
/**
* 读取数据
* 先根据默认值得到保存的数据的具体类型,然后调用对应的方法获取值
* @param context 上下文环境
* @param key 存储节点名称
* @param defaultObject 没有此节点时得到的默认值
* @return 默认值或者从节点读取到的值
*/
public static Object ReadSpValue(Context context , String key, Object defaultObject){
if (sp==null) {
sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
}
String type = defaultObject.getClass().getSimpleName();
if("String".equals(type)){
return sp.getString(key, (String)defaultObject);
}
else if("Integer".equals(type)){
return sp.getInt(key, (Integer)defaultObject);
}
else if("Boolean".equals(type)){
return sp.getBoolean(key, (Boolean)defaultObject);
}
else if("Float".equals(type)){
return sp.getFloat(key, (Float)defaultObject);
}
else if("Long".equals(type)){
return sp.getLong(key, (Long)defaultObject);
}
return null;
}
}
数据保存实例:
SpUtil.WriteSpValue(getApplicationContext(), "String", "xiaanming");
SpUtil.WriteSpValue(getApplicationContext(), "int", 10);
SpUtil.WriteSpValue(getApplicationContext(), "boolean", true);
SpUtil.WriteSpValue(getApplicationContext(), "long", 100L);
SpUtil.WriteSpValue(getApplicationContext(), "float", 1.1f);
数据读取实例:
SpUtil.ReadSpValue(getApplicationContext(), "String", "");
SpUtil.ReadSpValue(getApplicationContext(), "int", 0);
SpUtil.ReadSpValue(getApplicationContext(), "boolean", false);
SpUtil.ReadSpValue(getApplicationContext(), "long", 0L);
SpUtil.ReadSpValue(getApplicationContext(), "float", 0.0f);
上面读写方法里的key最好再单独定义一个类来存放,这样就保证在使用的时候不会出现写错key而取不到数据的情况
package com.zgs.mobilesafe.utils;
public class ConstantValue {
/**
* 是否开启更新的key
*/
public static final String OPEN_UPDATE = "open_update";
}
数据保存实例:
SpUtil.ReadSpValue(getApplicationContext(), ConstantValue.OPEN_UPDATE,false);
数据读取实例:
boolean open_update = SpUtil.WriteSpValue(this, ConstantValue.OPEN_UPDATE, false);