缓存在app应用中的使用
1,MyApplication
//声明
Map dataMap;
MyApplication的onCreate中使用
public void onCreate() {
super.onCreate();
dataMap = new HashMap<String, Object>();
}
2,缓存管理类cachMapManager
cachMapManager {
//put 缓存
public synchronized static void putCache(String key, Object object) {
if (MyApplication.dataMap==null) {
MyApplication.dataMap = new HashMap<String, Object>();
}
MyApplication.dataMap.put(key, object);
}
//get 缓存
public static Object getCache(Context context,String key) {
if (MyApplication.dataMap==null) {
MyApplication.dataMap = new HashMap<String, Object>();
}
Object obj=MyApplication.dataMap.get(key);
if (obj==null) {
Util.toRestart(context);
Logout.e("aaa", "缓存数据丢失,重启程序包");
}
return obj;
//是否可以为空
public static Object getCache(Context context,String key,boolean canEmpty) {
if (MyApplication.dataMap==null) {
MyApplication.dataMap = new HashMap<String, Object>();
}
Object obj=MyApplication.dataMap.get(key);
if (!canEmpty && obj==null) {
Util.toRestart(context);
Logout.e("aaa", "缓存数据丢失,重启程序包");
}
return obj;
}
}
3,在其他的activity中,设置缓存有使用
private class AAAActivity (){
//使用缓存信息
PresentCachInfo info = (PresentCachInfo)cacheMapManager.getCache(context,"presentCachinfo");
name = info.getName
/**
*设置缓存信息
/
setCach(){
//实体类
PresentCachInfo info = new PresentCachInfo();
//set信息
info.setName(zhang);
cachMapManager.putCach("presentCachinfo",info);
}
}