将对象存储到SharedPreferences里
来源:互联网 发布:ubuntu防火墙 编辑:程序博客网 时间:2024/05/21 21:42
原理:
需要将对象序列化,然后将序列化后的对象以字符串的方式保存在手机中
具体代码如下
package com.bestours.youlun.utils;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.text.TextUtils;import android.util.Log;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;/** * Created by sunny on 16/4/7. */public class SPUtil { /** * 从起启动应用,保存的搜索条件清空 * * @param mContext */ public static void initOption(Activity mContext) { SPUtil.saveObject(mContext, "destinationShow", null); SPUtil.saveObject(mContext, "companyShow", null); SPUtil.saveObject(mContext, "nightShow", null); SPUtil.saveObject(mContext, "dayShow", null); } /** * desc:获取保存的Object对象 * * @param context * @param key * @return modified: */ public static Object readObject(Context context, String key) { Object readObject = null; try { SharedPreferences sharedata = context.getSharedPreferences("config", 0); if (sharedata.contains(key)) { String string = sharedata.getString(key, ""); if (TextUtils.isEmpty(string)) { readObject = null; } else { //将16进制的数据转为数组,准备反序列化 byte[] stringToBytes = StringToBytes(string); ByteArrayInputStream bis = new ByteArrayInputStream(stringToBytes); ObjectInputStream is = new ObjectInputStream(bis); //返回反序列化得到的对象 readObject = is.readObject(); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); readObject = null; } //所有异常返回null return readObject; } /** * desc:将16进制的数据转为数组 * <p>创建人:聂旭阳 , 2014-5-25 上午11:08:33</p> * * @param data * @return modified: */ public static byte[] StringToBytes(String data) { String hexString = data.toUpperCase().trim(); if (hexString.length() % 2 != 0) { return null; } byte[] retData = new byte[hexString.length() / 2]; for (int i = 0; i < hexString.length(); i++) { int int_ch; // 两位16进制数转化后的10进制数 char hex_char1 = hexString.charAt(i); ////两位16进制数中的第一位(高位*16) int int_ch1; if (hex_char1 >= '0' && hex_char1 <= '9') int_ch1 = (hex_char1 - 48) * 16; //// 0 的Ascll - 48 else if (hex_char1 >= 'A' && hex_char1 <= 'F') int_ch1 = (hex_char1 - 55) * 16; //// A 的Ascll - 65 else return null; i++; char hex_char2 = hexString.charAt(i); ///两位16进制数中的第二位(低位) int int_ch2; if (hex_char2 >= '0' && hex_char2 <= '9') int_ch2 = (hex_char2 - 48); //// 0 的Ascll - 48 else if (hex_char2 >= 'A' && hex_char2 <= 'F') int_ch2 = hex_char2 - 55; //// A 的Ascll - 65 else return null; int_ch = int_ch1 + int_ch2; retData[i / 2] = (byte) int_ch;//将转化后的数放入Byte里 } return retData; } /** * desc:保存对象 * * @param context * @param key * @param obj 要保存的对象,只能保存实现了serializable的对象 * modified: */ public static void saveObject(Context context, String key, Object obj) { try { // 保存对象 SharedPreferences.Editor sharedata = context.getSharedPreferences("config", 0).edit(); //先将序列化结果写到byte缓存中,其实就分配一个内存空间 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(bos); //将对象序列化写入byte缓存 os.writeObject(obj); //将序列化的数据转为16进制保存 String bytesToHexString = bytesToHexString(bos.toByteArray()); //保存该16进制数组 sharedata.putString(key, bytesToHexString); sharedata.commit(); } catch (IOException e) { e.printStackTrace(); Log.e("", "保存obj失败"); } } /** * desc:将数组转为16进制 * * @param bArray * @return modified: */ public static String bytesToHexString(byte[] bArray) { if (bArray == null) { return null; } if (bArray.length == 0) { return ""; } StringBuffer sb = new StringBuffer(bArray.length); String sTemp; for (int i = 0; i < bArray.length; i++) { sTemp = Integer.toHexString(0xFF & bArray[i]); if (sTemp.length() < 2) sb.append(0); sb.append(sTemp.toUpperCase()); } return sb.toString(); }}
0 0
- 将对象存储到SharedPreferences里
- Android将对象存储到SharedPreferences中
- Android将对象存储到SharedPreferences中
- SharedPreferences 存储(将数据存储到 SharedPreferences 中)
- 将数据存储到SharedPreferences中
- Android 将数据存储到SharedPreferences 中
- android 将简单的数据存储到本地-SharedPreferences
- android SharedPreferences 存储对象
- SharedPreferences 存储对象
- android SharedPreferences 存储对象
- android SharedPreferences 存储对象
- SharedPreferences 存储对象
- SharedPreferences 存储对象
- android SharedPreferences 存储对象
- 将数据储存到SharedPreferences
- SharedPreferences存储到指定位置
- SharedPreferences 存储复杂的对象
- Android SharedPreferences 存储复杂对象
- 快速幂——poj1995
- 数学老师必备工具,你的最爱!
- DDMenu中添加手势是否有用的方法,为了在视图中添加手势起作用
- 使用jar命令搜索jar中的class文件(shell)
- android 去掉反斜杠
- 将对象存储到SharedPreferences里
- 搭建个人网站
- 大端法、小端法、网络字节序 转
- 课件动画做的牛不牛,看你有它没它!
- Android实现自己的回调函数
- IOS UTI(统一类型标识符) 根据文件后缀打开APP
- axiom解析xml
- 《Fully Convolutional Network for Semantic Segmentation》的阅读分享
- Log4j.properties配置详解