android sharedpreferences 存储对象,SharedPreferences存储复杂对象解决方案

/**

* SharedPreferences工具类

*

* @author bobby

*

*/

public class SharedPreferencesUtils {

Context context;

String name;

public SharedPreferencesUtils(Context context, String name) {

this.context = context;

this.name = name;

}

/**

* 根据key和预期的value类型获取value的值

*

* @param key

* @param clazz

* @return

*/

public T getValue(String key, Class clazz) {

if (context == null) {

throw new RuntimeException("请先调用带有context,name参数的构造!");

}

SharedPreferences sp = this.context.getSharedPreferences(this.name, Context.MODE_PRIVATE);

return getValue(key, clazz, sp);

}

/**

* 针对复杂类型存储

*

* @param key

* @param val

*/

public void setObject(String key, Object object) {

SharedPreferences sp = this.context.getSharedPreferences(this.name, Context.MODE_PRIVATE);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ObjectOutputStream out = null;

try {

out = new ObjectOutputStream(baos);

out.writeObject(object);

String objectVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));

Editor editor = sp.edit();

editor.putString(key, objectVal);

editor.commit();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (baos != null) {

baos.close();

}

if (out != null) {

out.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

@SuppressWarnings("unchecked")

public T getObject(String key, Class clazz) {

SharedPreferences sp = this.context.getSharedPreferences(this.name, Context.MODE_PRIVATE);

if (sp.contains(key)) {

String objectVal = sp.getString(key, null);

byte[] buffer = Base64.decode(objectVal, Base64.DEFAULT);

ByteArrayInputStream bais = new ByteArrayInputStream(buffer);

ObjectInputStream ois = null;

try {

ois = new ObjectInputStream(bais);

T t = (T) ois.readObject();

return t;

} catch (StreamCorruptedException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} finally {

try {

if (bais != null) {

bais.close();

}

if (ois != null) {

ois.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

return null;

}

/**

* 对于外部不可见的过渡方法

*

* @param key

* @param clazz

* @param sp

* @return

*/

@SuppressWarnings("unchecked")

private T getValue(String key, Class clazz, SharedPreferences sp) {

T t;

try {

t = clazz.newInstance();

if (t instanceof Integer) {

return (T) Integer.valueOf(sp.getInt(key, 0));

} else if (t instanceof String) {

return (T) sp.getString(key, "");

} else if (t instanceof Boolean) {

return (T) Boolean.valueOf(sp.getBoolean(key, false));

} else if (t instanceof Long) {

return (T) Long.valueOf(sp.getLong(key, 0L));

} else if (t instanceof Float) {

return (T) Float.valueOf(sp.getFloat(key, 0L));

}

} catch (InstantiationException e) {

e.printStackTrace();

Log.e("system", "类型输入错误或者复杂类型无法解析[" + e.getMessage() + "]");

} catch (IllegalAccessException e) {

e.printStackTrace();

Log.e("system", "类型输入错误或者复杂类型无法解析[" + e.getMessage() + "]");

}

Log.e("system", "无法找到" + key + "对应的值");

return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值