android 对象保存到SP
1.工具类
package com.you.me.utils;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import android.widget.TextView;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author cao_zhi_qing
* @version Revision: 1.0 Date: 2015/1/2
*/
public class ContextUtils {
/**
* 把对象保存到以String形式保存到sp中
* @param context 上下文
* @param t 泛型参数
* @param spName sp文件名
* @param keyName 字段名
* @param
*/
public static void saveObj2SP(Context context, T t, String spName, String keyName) {
SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE); ByteArrayOutputStream bos; ObjectOutputStream oos = null; try { bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(t); byte[] bytes = bos.toByteArray();
// String ObjStr = new String(bytes,0,bytes.length);
String ObjStr = Base64.encodeToString(bytes, Base64.DEFAULT);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(keyName, ObjStr);
editor.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
if (oos != null) {
try {
oos.flush();
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
} }}/** * sp文件名 和 字段名相同 * @param context * @param t * @param spName * @param */public static void saveObj2SP(Context context, T t, String spName) { saveObj2SP(context, t, spName, spName);}/** *从sp中读取对象 * @param context * @param spName sp文件名 * @param keyNme 字段名 * @param 泛型参数 * @return */public static T getObjFromSp(Context context, String spName, String keyNme) { SharedPreferences preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE); byte[] bytes = Base64.decode(preferences.getString(keyNme, ""), Base64.DEFAULT); ByteArrayInputStream bis; ObjectInputStream ois = null; T obj = null; try { bis = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bis); obj = (T) ois.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (ois != null) { try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } } return obj;}public static T getObjFromSp(Context context, String spName){ return getObjFromSp(context,spName,spName);}
}
2.被保存的对象要先序列化 Serializable。
package com.example.customView;
import java.io.Serializable;
/**
* @author cao_zhi_qing
* @version Revision: 1.0 Date: 2015/6/29
* @Description:请用一句话描述此类
*/
public class Entity implements Serializable {
private String name;private String address;public String getName() { return name;}public void setName(String name) { this.name = name;}public String getAddress() { return address;}public void setAddress(String address) { this.address = address;}
}
3保存对象、获取对象。
package com.example.customView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
private TitleView titleView ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Entity entity = new Entity();
entity.setName(“name”);
entity.setAddress(“address”);
ContextUtils.saveObj2SP(this,entity,”entity”);
Entity entity1 = ContextUtils.getObjFromSp(this,"entity"); System.out.println(entity1.getName()); System.out.println(entity1.getAddress());}
}
更多相关文章
- android TabHost 对象报错
- Android wifi的WifiInfo对象详解
- Android之JAVASe基础篇-面向对象-IO(九)
- android 随手记之文件+参数上传请求
- Android中,把XML文件转换成Object对象的方法
- 初学Android,五大布局对象(六)
- 根据文件名称修改安卓默认的蓝牙接收文件地址
- 关于Android设备屏幕大小及密度的系统参数类
- Android中intent传递对象和Bundle的用法