一、创建缓存类CachePut.java
package com.etongwl.idsphoto.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
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();
}
}
二、创建获取类CacheGet.java
package com.etongwl.idsphoto.utils;
import android.content.Context;
import android.content.SharedPreferences;
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;
}
}
三、在Activity中使用方式
1.首先声明
CachePut cachePut = new CachePut();
CacheGet cacheGet = new CacheGet();
存储String数据:cachePut.putCacheStringG(context, spName, key, value)
获取存储的String数据:cacheGet.getCacheStringG(context ,spName ,key)
同理可以对Int 、Long、Boolean等数据进行存储
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119217.html