利用sharedpreferences缓存应用数据的工具类
public classCacheUtils {
private final staticStringSP_NAME="XXXXXXXXX";
private staticSharedPreferencessp;
private staticSharedPreferencesgetSp(Context context) {
if(sp==null) {
sp= context.getSharedPreferences(SP_NAME,Context.MODE_PRIVATE);
}
returnsp;
}
/**
* 获取boolean 数据
*
*@paramcontext
*@paramkey
*@return如果没有值,返回false
*/
public static booleangetBoolean(Context context,String key) {
SharedPreferences sp =getSp(context);
returnsp.getBoolean(key, false);
}
/**
* 获取boolean 数据
*
*@paramcontext
*@paramkey
*@paramdefValue
*@return
*/
public static booleangetBoolean(Context context,String key,
booleandefValue) {
SharedPreferences sp =getSp(context);
returnsp.getBoolean(key,defValue);
}
/**
* 存boolean缓存
*
*@paramcontext
*@paramkey
*@paramvalue
*/
public static voidsetBoolean(Context context,String key, booleanvalue) {
SharedPreferences sp =getSp(context);
Editor editor = sp.edit();
editor.putBoolean(key,value);
editor.commit();
}
/**
* 获取String 数据
*
*@paramcontext
*@paramkey
*@return如果没有值,返回null
*/
public staticStringgetString(Context context,String key) {
SharedPreferences sp =getSp(context);
returnsp.getString(key, null);
}
/**
* 获取String 数据
*
*@paramcontext
*@paramkey
*@paramdefValue
*@return
*/
public staticStringgetString(Context context,String key,String defValue) {
SharedPreferences sp =getSp(context);
returnsp.getString(key,defValue);
}
/**
* 存String缓存
*
*@paramcontext
*@paramkey
*@paramvalue
*/
public static voidsetString(Context context,String key,String value) {
SharedPreferences sp =getSp(context);
Editor editor = sp.edit();
editor.putString(key,value);
editor.commit();
}
/**
* 获取int 数据
*
*@paramcontext
*@paramkey
*@return如果没有值,返回-1
*/
public static intgetInt(Context context,String key) {
SharedPreferences sp =getSp(context);
returnsp.getInt(key,-1);
}
/**
* 获取int 数据
*
*@paramcontext
*@paramkey
*@paramdefValue
*@return
*/
public static intgetInt(Context context,String key, intdefValue) {
SharedPreferences sp =getSp(context);
returnsp.getInt(key,defValue);
}
/**
* 存int缓存
*
*@paramcontext
*@paramkey
*@paramvalue
*/
public static voidsetInt(Context context,String key, intvalue) {
SharedPreferences sp =getSp(context);
Editor editor = sp.edit();
editor.putInt(key,value);
editor.commit();
}
public staticStringSceneList2String(List SceneList)throwsIOException {
// 实例化一个ByteArrayOutputStream对象,用来装载压缩后的字节文件。
ByteArrayOutputStream byteArrayOutputStream =newByteArrayOutputStream();
// 然后将得到的字符数据装载到ObjectOutputStream
ObjectOutputStream objectOutputStream =newObjectOutputStream(
byteArrayOutputStream);
// writeObject 方法负责写入特定类的对象的状态,以便相应的 readObject 方法可以还原它
objectOutputStream.writeObject(SceneList);
// 最后,用Base64.encode将字节文件转换成Base64编码保存在String中
String SceneListString =newString(Base64.encode(
byteArrayOutputStream.toByteArray(),Base64.DEFAULT));
// 关闭objectOutputStream
objectOutputStream.close();
returnSceneListString;
}
@SuppressWarnings("unchecked")
public staticListString2SceneList(String SceneListString)
throwsStreamCorruptedException,IOException,
ClassNotFoundException {
byte[] mobileBytes = Base64.decode(SceneListString.getBytes(),
Base64.DEFAULT);
ByteArrayInputStream byteArrayInputStream =newByteArrayInputStream(
mobileBytes);
ObjectInputStream objectInputStream =newObjectInputStream(
byteArrayInputStream);
List SceneList = (List) objectInputStream.readObject();
objectInputStream.close();
returnSceneList;
}
}