博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 保存浏览记录 SharedPreTools
阅读量:6116 次
发布时间:2019-06-21

本文共 6237 字,大约阅读时间需要 20 分钟。

hot3.png

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 
List
readAllServicePre(String per_name){ SharedPreferences loginUserInfo = EdjApp.getInstance(). getSharedPreferences(per_name, Context.MODE_APPEND); Map
maps = loginUserInfo.getAll();//取出所有数据 List
oblist = new ArrayList
(); 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
tempserviceInfo = new ArrayList
(); tempserviceInfo.addAll((Collection
) oblist); Comparator
ascComparator = new ServiceInfoComparator(); Collections.sort(tempserviceInfo, ascComparator); oblist.clear(); oblist.addAll((Collection
) 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
List
readAllObjectPre(String per_name){ SharedPreferences loginUserInfo = EdjApp.getInstance(). getSharedPreferences(per_name, Context.MODE_APPEND); Map
maps = loginUserInfo.getAll();//取出所有数据 List
oblist = new ArrayList
(); 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
{ @Override public int compare(ServiceInfo object1, ServiceInfo object2) { // TODO Auto-generated method stub return Double.compare(object2.getSaveTime(), object1.getSaveTime()); }}

转载于:https://my.oschina.net/oppo4545/blog/197700

你可能感兴趣的文章
linux系统安装的引导镜像制作流程分享
查看>>
解决ros建***能登录不能访问内网远程桌面的问题
查看>>
pfsense锁住自己
查看>>
vsftpd 相关总结
查看>>
bash complete -C command
查看>>
解决zabbix 3.0中1151端口不能运行问题
查看>>
售前工程师的成长---一个老员工的经验之谈
查看>>
Get到的优秀博客网址
查看>>
dubbo
查看>>
【Git入门之四】操作项目
查看>>
老男孩教育每日一题-第107天-简述你对***的理解,常见的有哪几种?
查看>>
Python学习--time
查看>>
在OSCHINA上的第一篇博文,以后好好学习吧
查看>>
高利率时代的结局,任重道远,前途叵测
查看>>
Debian 6.05安装后乱码
查看>>
欢迎大家观看本人录制的51CTO精彩视频课程!
查看>>
IntelliJ IDEA中设置忽略@param注释中的参数与方法中的参数列表不一致的检查
查看>>
关于软件开发的一些感悟
查看>>
uva 10806
查看>>
纯CSS3绘制的黑色图标按钮组合
查看>>