1.0存储类
/*
* 缓存数据
* 用之前要实例化
* **/
public class CachePut
{
public void putCacheInterG(Context context, String spName, String key, int value) {
putCache(context, spName, key, value);
}
public void putCacheStringG(Context context, String spName, String key, String value) {
putCache(context, spName, key, value);
}
public void putCacheBooleanG(Context context, String spName, String key, Boolean value) {
putCache(context, spName, key, value);
}
public void putCacheLongG(Context context, String spName, String key, long value) {
putCache(context, spName, key, value);
}
private static void putCache(Context context, String spName, String key, String value) {
SharedPreferences sp = context.getSharedPreferences(spName, 0);
sp.edit().putString(key, value).commit();
}
public static void putCache(Context context, String spName, String key, int value) {
SharedPreferences sp = context.getSharedPreferences(spName, 0);
sp.edit().putInt(key, value).commit();
}
public static void putCache(Context context, String spName, String key, Boolean value) {
SharedPreferences sp = context.getSharedPreferences(spName, 0);
sp.edit().putBoolean(key, value.booleanValue()).commit();
}
public static void putCache(Context context, String spName, String key, long value) {
SharedPreferences sp = context.getSharedPreferences(spName, 0);
sp.edit().putLong(key, value).commit();
}
}
2. 获取数据
/*
* 获取缓存数据
* 用之前要实例化
* **/
public class CacheGet{
public int getCacheIntegerG(Context context, String cacheName, String key) {
return getCacheInteger(context, cacheName, key);
}
public String getCacheStringG(Context context, String cacheName, String key) {
return getCacheString(context, cacheName, key);
}
public long getCacheLongG(Context context, String cacheName, String key) {
return getCacheLong(context, cacheName, key);
}
public boolean getCacheBooleanG(Context context, String cacheName, String key) {
return getCacheBoolean(context, cacheName, key);
}
private int getCacheInteger(Context context, String cacheName, String key) {
SharedPreferences sp = context.getSharedPreferences(cacheName, 0);
int receive = sp.getInt(key, 0);
return receive;
}
private static String getCacheString(Context context, String cacheName, String key) {
SharedPreferences sp = context.getSharedPreferences(cacheName, 0);
String receive = sp.getString(key, "");
return receive;
}
private long getCacheLong(Context context, String cacheName, String key) {
SharedPreferences sp = context.getSharedPreferences(cacheName, 0);
long receive = sp.getLong(key, 0L);
return receive;
}
private boolean getCacheBoolean(Context context, String cacheName, String key) {
SharedPreferences sp = context.getSharedPreferences(cacheName, 0);
boolean receive = sp.getBoolean(key, false);
return receive;
}
}
3.Activity中使用
//实例化
private CachePut cachePut = new CachePut();
//缓存激活信息
cachePut.putCacheStringG(mContext, Configs.CACHE_NAME, Configs.CACHE_BEGIN_TIME_KEY, begin_time);
cachePut.putCacheStringG(mContext, Configs.CACHE_NAME, Configs.CACHE_CURRENT_TIME_KEY, currentTime);
cachePut.putCacheStringG(mContext, Configs.CACHE_NAME, Configs.CACHE_END_TIME_KEY, end_time);