android 对象保存到SP
来源:互联网 发布:linux编辑文件的命令 编辑:程序博客网 时间:2024/04/29 02:21
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 <T> */public static <T> void saveObj2SP(Context context, T t, String spName) { saveObj2SP(context, t, spName, spName);}/** *从sp中读取对象 * @param context * @param spName sp文件名 * @param keyNme 字段名 * @param <T> 泛型参数 * @return */public static <T extends Object> 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 extends Object> 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 对象保存到SP
- 用户列表List保存到SP文件
- Android中用SP(SharedPreferences)保存数据
- Android framework回顾(2) sp 和 wp sp对象
- 将对象保存到文件
- java对象保存到文件
- NSKeyedArchiver保存对象到本地
- 保存对象到文件中
- 【Hibernate】保存对象到数据库
- Android 保存到文件
- android sharedpreference 保存对象
- 保存Graphics对象到Bitmap对象
- [转帖]保存Graphics对象到Bitmap对象
- Android sp & sp
- Android sp & sp
- android 保存图片到数据库
- android 保存图片到SQLite
- android 保存图片到SQLite
- 回溯算法——算法总结(四)
- memcached中的一些参数限制
- JDBC-Sql防注入
- DuiLib 消息机制剖析
- 天声人語 20150629
- android 对象保存到SP
- ArcGIS for js API 3.6
- 第十七周课程实习-猜猜看游戏(前期准备)
- 让我们一起Go(六)
- 什么是App加壳,以及App加壳的利与弊
- 让我们一起Go(七)
- OJ第二批——Problem K:填空题:类模板---求数组的最大值
- 让我们一起Go(八)
- UIViewController的生命周期及iOS程序执行顺序