public class SharedPreTools {
//单键值存储----------------------------
public static String readShare(String sharename,String key) {
SharedPreferences user = EdjApp.getInstance().getSharedPreferences(sharename,0);
return user.getString(key, "");
}
public static void writeShare(String sharename,String key,String value ) {
SharedPreferences user = EdjApp.getInstance().getSharedPreferences(sharename, 0);
Editor editor = user.edit();
editor.putString(key, value);
editor.commit();
}
public static void removeShare(String sharename,String key ) {
SharedPreferences user = EdjApp.getInstance().getSharedPreferences(sharename, 0);
Editor editor = user.edit();
editor.remove(key);
editor.commit();
}
//多键值存储
public static void writegroup(String sharename,String key,String value) {
SharedPreferences call = EdjApp.getInstance().getSharedPreferences(sharename, 0);
String group = call.getString(key, "");
if (group.indexOf(value) < 0) {
String newgroup = group + "," + key;
Editor editor = call.edit();
editor.putString(key, newgroup);
editor.commit();
}
}
public static void removegroup(String sharename,String key) {
SharedPreferences call = EdjApp.getInstance().getSharedPreferences(sharename, 0);
String group = call.getString(key, "");
String newgroup = group.replace("," + key, "");
Editor editor = call.edit();
editor.putString(key, newgroup);
editor.commit();
}
/**
* 储存单条对象 serviceInfo
* @param per_name
* @param key
* @param vaule
*/
public static void saveServicePre(String per_name,String key,Object vaule){
SharedPreferences loginUserInfo = EdjApp.getInstance().
getSharedPreferences(per_name, Context.MODE_APPEND);
Editor preEd = null;
if(loginUserInfo != null){
preEd = loginUserInfo.edit();
}
if(preEd != null){
if(vaule != null){
ServiceInfo info1=(ServiceInfo)vaule;
info1.setSaveTime(getTimeId());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos=null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(info1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String info = new String(Base64.encodeBase64(baos.toByteArray()));
preEd.putString(key, info);
}
preEd.commit();
}
}
/**
* 读取对象集合 serviceInfo
* @param per_name
* @param oblist
*/
public static <T> List<T> readAllServicePre(String per_name){
SharedPreferences loginUserInfo = EdjApp.getInstance().
getSharedPreferences(per_name, Context.MODE_APPEND);
Map<String, ?> maps = loginUserInfo.getAll();//取出所有数据
List<T> oblist = new ArrayList<T>();
Iterator it = maps.values().iterator();
while(it.hasNext()){
String base64Str = (String) it.next();
byte[] base64Bytes = Base64.decodeBase64(base64Str .getBytes());
ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
ObjectInputStream ois;
try {
ois = new ObjectInputStream(bais);
T tempobj = (T) ois.readObject();
oblist.add(tempobj);
// LoginUser loginUser = (LoginUser)tempobj;
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ArrayList<ServiceInfo>tempserviceInfo = new ArrayList<ServiceInfo>();
tempserviceInfo.addAll((Collection<? extends ServiceInfo>) oblist);
Comparator<ServiceInfo> ascComparator = new ServiceInfoComparator();
Collections.sort(tempserviceInfo, ascComparator);
oblist.clear();
oblist.addAll((Collection<? extends T>) tempserviceInfo);
return oblist;
}
/**
* 删除单条对象数据
* @param per_name
* @param key
*/
public static void removeObjectPre(String per_name,String key){
SharedPreferences loginUserInfo = EdjApp.getInstance().
getSharedPreferences(per_name, Context.MODE_APPEND);
Editor preEd = null;
if(loginUserInfo != null){
preEd = loginUserInfo.edit();
}
preEd.remove(key);
preEd.commit();
}
//当前时间为id
public static long getTimeId() {
Date data = new Date();
long time=data.getTime();
return time;
}
//---------------------------
/**
* 储存单条对象
* @param per_name
* @param key
* @param vaule
*/
public static void saveObjectPre(String per_name,String key,Object vaule){
SharedPreferences loginUserInfo = EdjApp.getInstance().
getSharedPreferences(per_name, Context.MODE_APPEND);
Editor preEd = null;
if(loginUserInfo != null){
preEd = loginUserInfo.edit();
}
if(preEd != null){
if(vaule != null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos=null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(vaule);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String info = new String(Base64.encodeBase64(baos.toByteArray()));
preEd.putString(key, info);
}
preEd.commit();
}
}
/**
* 读取对象集合
* @param per_name
* @param oblist
*/
public static <T> List<T> readAllObjectPre(String per_name){
SharedPreferences loginUserInfo = EdjApp.getInstance().
getSharedPreferences(per_name, Context.MODE_APPEND);
Map<String, ?> maps = loginUserInfo.getAll();//取出所有数据
List<T> oblist = new ArrayList<T>();
Iterator it = maps.values().iterator();
while(it.hasNext()){
String base64Str = (String) it.next();
byte[] base64Bytes = Base64.decodeBase64(base64Str .getBytes());
ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
ObjectInputStream ois;
try {
ois = new ObjectInputStream(bais);
T tempobj = (T) ois.readObject();
oblist.add(tempobj);
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return oblist;
}
象的排序,需要 根据设置的时间来算
public class ServiceInfoComparator implements Comparator<ServiceInfo> {
@Override
public int compare(ServiceInfo object1, ServiceInfo object2) {
// TODO Auto-generated method stub
return Double.compare(object2.getSaveTime(), object1.getSaveTime());
}
}