需求分析
本文介绍一个SharedPreference的使用的工具类,在Android开发的过程中不可避免的会使用SharedPreference来缓存一个数据,(1)如果频繁使用的话还是最好把存取操作放在子线程去处理;(2)如果不写一个关于SharedPerference的工具类,没法复用很多操作,(3)如果每个对应的业务的数据都写对应的存取删除操作的话,到项目后期会有很多,难以维护;
针对上面的三个问题实现一个比较通用可扩展性强的工具里,SharedPerference支持六种数据:String,Boolean,Int,Long,Float和Set
SharePerferenceUtil的包含两个内部类:FILE_KEYS和VALUE_KEYS作为索引。
public class SPUtil {
public static final class FILE_KEYS{
public static final String APP_STATUS = "app_status";
public static final String USER_INFO = "user_info";
}
public static final class VALUE_KEYS{
public static final String IS_LOGIN = "is_login";
public static final String USER = "user";
public static final String MOBILE_LOGIN_LIST = "mobile_login_list";
}
...
读取成功后的回调接口:
public interface SPResultListener {
void onResult(T t);
}
当存取List数据时需要Id作为唯一标识,所以Object必须实现接口:
public interface ShareCacheableData {
String getId();
}
1、首先基本数据类型,以Boolean为例:
存数据,调用RxJava使得写操作在I/O线程中进行:
public static final synchronized void saveBoolean(final String fileKey, final String valueKey, final boolean value){
Observable.create(new Observable.OnSubscribe
读数据,调用RxJava使得读取操作在I/O线程中进行,读取成功后在UI线程中进行的回调:
public static final void getBoolean(final String fileKey, final String valueKey, final SPResultListener resultListener) {
Observable.fromCallable(new Callable() {
@Override
public Boolean call() throws Exception {
SharedPreferences sp = OpenEyeApplication.getInstance().getSharedPreferences(fileKey, Context.MODE_PRIVATE);
return sp.getBoolean(valueKey, false);
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1() {
@Override
public void call(Boolean value) {
value = (value == null) ? false:value;
if (resultListener != null) {
resultListener.onResult(value);
}
}
});
}
2、存取实体对象,和存取基本的数据多了一个转String的过程,
保存实体对象:
public static final void saveModel(final String fileKey, final String valueKey, final T t){
Observable.create(new Observable.OnSubscribe
读取实体对象:
public static final void getModel(final String fileKey, final String valueKey, final Class clazz, final SPResultListener resultListener) {
Observable.fromCallable(new Callable() {
@Override
public T call() throws Exception {
SharedPreferences sp = OpenEyeApplication.getInstance().getSharedPreferences(fileKey, Context.MODE_PRIVATE);
String sResult = sp.getString(valueKey, "");
if (TextUtils.isEmpty(sResult)) {
return null;
} else {
T t = new Gson().fromJson(sResult, clazz);
return t;
}
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1() {
@Override
public void call(T value) {
if (resultListener != null) {
resultListener.onResult(value);
}
}
});
}
2、存取实体对象列表,Gson转换的时候容易出错,可以特别注意一下:
往列表保存对象,ShareCacheableData是为了获取对象的唯一标识:
public static final void putList(final String fileKey, final String valueKey, final Class clazz, final T t){
Observable.create(new Observable.OnSubscribe
读取对象列表:
public static final void getList(final String fileKey, final String valueKey, final Class clazz, final SPResultListener> resultListener) {
Observable.fromCallable(new Callable>() {
@Override
public List call() throws Exception {
SharedPreferences sp = OpenEyeApplication.getInstance().getSharedPreferences(fileKey, Context.MODE_PRIVATE);
String sResult = sp.getString(valueKey, "");
if (TextUtils.isEmpty(sResult)) {
return null;
} else {
T[] list = new Gson().fromJson(sResult, clazz);
return new ArrayList(Arrays.asList(list));
}
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1>() {
@Override
public void call(List value) {
if (resultListener != null) {
resultListener.onResult(value);
}
}
});
}
结束了。。。