SharedPreferences 存储实体类
2017-11-17 14:25
232 查看
首先SharedPreferences 存取的实体类不能太大,太大的还是建议存数据库。一般存取的实体类是用户喜好的封装类。
实现代码
/**
* 存放实体类以及任意类型
* @param context 上下文对象
* @param key
* @param obj
*/
public static void putBean(Context context, String key, Object obj) {
if (obj instanceof Serializable) {// obj必须实现Serializable接口,否则会出问题
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
String string64 = new String(Base64.encode(baos.toByteArray(),0));
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString(key, string64).commit();
} catch (IOException e) {
e.printStackTrace();
}
} else {
throw new IllegalArgumentException("the obj must implement Serializble");
}
}
public static Object getBean(Context context, String key) {
Object obj = null;
try {
String base64 = getSharedPreferences(context).getString(key, "");
if (base64.equals("")) {
return null;
}
byte[] base64Bytes = Base64.decode(base64.getBytes(), 1);
ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
obj = ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return obj;}参考文章:http://blog.csdn.net/u014626094/article/details/47206263
实现代码
/**
* 存放实体类以及任意类型
* @param context 上下文对象
* @param key
* @param obj
*/
public static void putBean(Context context, String key, Object obj) {
if (obj instanceof Serializable) {// obj必须实现Serializable接口,否则会出问题
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
String string64 = new String(Base64.encode(baos.toByteArray(),0));
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString(key, string64).commit();
} catch (IOException e) {
e.printStackTrace();
}
} else {
throw new IllegalArgumentException("the obj must implement Serializble");
}
}
public static Object getBean(Context context, String key) {
Object obj = null;
try {
String base64 = getSharedPreferences(context).getString(key, "");
if (base64.equals("")) {
return null;
}
byte[] base64Bytes = Base64.decode(base64.getBytes(), 1);
ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
obj = ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return obj;}参考文章:http://blog.csdn.net/u014626094/article/details/47206263
相关文章推荐
- Android开发笔记——SharedPreferences 存储实体类以及任意类型
- Android开发必备技能之Android数据存储(SharedPreference存储)
- SharedPreferences 存储(将数据存储到 SharedPreferences 中)
- Android之SharedPreference轻量级数据存储
- 数据库存储之Sharedpreference和SQLite
- Android的移动存储之SharedPreferences <可保存复杂数据>
- SharedPreference存储的使用
- Android之SharedPreference轻量级数据存储
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- Android开发:SharedPreferences 存储数据、获取数据
- SharedPreferences 存储数组+双击退出
- Android之SharedPreference存储一个对象
- SharedPreferences 存储集合数据丢失的问题
- Android 存储(本地存储 SD卡存储 SharedPreference SQLite ContentProvider)
- 三大存储方式之一:SharedPreferences 存储
- SharedPreferences 存储对象
- android 使用SharedPreferences对数据存储/删除/读取/文件删除
- Android之SharedPreference轻量级数据存储
- android sharedpreferences StringSet不能真正存储数据的问题
- SharedPreferences 存储的注意事项