import com.eyeguard.app.application.EyeGuardApplication;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/**
* 主要功能:用于存储缓存数据
*@author caijin
*/
public class AppSharePreferenceMgr {
//定义sharedPreferences
private static SharedPreferences sharedPreferences;
//定义editor
private static Editor editor;
//传输数据类型判断
private static String CLASS_SIMPLENAME = null;
/**
* 清除缓存数据(默认缓存文件名称)
* @param context 上下文对象
*/
public static void cleanSharedPreferences(Context context){
sharedPreferences = context.getSharedPreferences(EyeGuardApplication.APP_SHARE_FILE, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.clear();
editor.commit();
}
/**
* 清除缓存数据(指定缓存文件名称)
* @param context 上下文对象
* @param fileKey 缓存文件名称(唯一标识)
*/
public static void cleanSharedPreferences(Context context, String fileKey){
sharedPreferences = context.getSharedPreferences(fileKey, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.clear();
editor.commit();
}
/**
* 不同数据类型缓存设置(默认缓存文件名称)<br/>
* AppSharePreferenceMgr.setParam(this, "key1", "caijin")<br/>
* AppSharePreferenceMgr.setParam(this, "key2", 10)<br/>
* AppSharePreferenceMgr.setParam(this, "key3", true)<br/>
* AppSharePreferenceMgr.setParam(this, "key4", 100L)<br/>
* AppSharePreferenceMgr.setParam(this, "key5", 1.1f)<br/>
* @param context 上下文对象
* @param key 设置KEY
* @param object 设置VALUE
*/
public static void setParam(Context context, String key, Object object){
sharedPreferences = context.getSharedPreferences(EyeGuardApplication.APP_SHARE_FILE, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
CLASS_SIMPLENAME = object.getClass().getSimpleName();
if("String".equals(CLASS_SIMPLENAME)){
editor.putString(key, (String) object);
editor.commit();
return;
}
if("Integer".equals(CLASS_SIMPLENAME)){
editor.putInt(key, (Integer) object);
editor.commit();
return;
}
if("Boolean".equals(CLASS_SIMPLENAME)){
editor.putBoolean(key, (Boolean) object);
editor.commit();
return;
}
if("Float".equals(CLASS_SIMPLENAME)){
editor.putFloat(key, (Float) object);
editor.commit();
return;
}
if("Long".equals(CLASS_SIMPLENAME)){
editor.putLong(key, (Long) object);
editor.commit();
return;
}
}
/**
* 不同数据类型缓存设置(指定缓存文件名称)<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key1", "caijin")<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key2", 10)<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key3", true)<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key4", 100L)<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key5", 1.1f)<br/>
* @param context 上下文对象
* @param fileKey 缓存文件名称(唯一标识)
* @param key 缓存KEY
* @param object 缓存VALUE
*/
public static void setParam(Context context, String fileKey, String key, Object object){
sharedPreferences = context.getSharedPreferences(fileKey, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
CLASS_SIMPLENAME = object.getClass().getSimpleName();
if("String".equals(CLASS_SIMPLENAME)){
editor.putString(key, (String) object);
editor.commit();
return;
}
if("Integer".equals(CLASS_SIMPLENAME)){
editor.putInt(key, (Integer) object);
editor.commit();
return;
}
if("Boolean".equals(CLASS_SIMPLENAME)){
editor.putBoolean(key, (Boolean) object);
editor.commit();
return;
}
if("Float".equals(CLASS_SIMPLENAME)){
editor.putFloat(key, (Float) object);
editor.commit();
return;
}
if("Long".equals(CLASS_SIMPLENAME)){
editor.putLong(key, (Long) object);
editor.commit();
return;
}
}
/**
* 不同数据类型缓存读取(默认缓存文件名称)<br/>
* AppSharePreferenceMgr.getParam(this, "key1", "caijin")<br/>
* AppSharePreferenceMgr.getParam(this, "key2", 0)<br/>
* AppSharePreferenceMgr.getParam(this, "key3", false)<br/>
* AppSharePreferenceMgr.getParam(this, "key4", 0L)<br/>
* AppSharePreferenceMgr.getParam(this, "key5", 0.0f)<br/>
* @param context 上下文对象
* @param key 获取KEY值
* @param defValue 默认数据值
* @return 实际数据值
*/
public static Object getParam(Context context, String key, Object defValue){
sharedPreferences = context.getSharedPreferences(EyeGuardApplication.APP_SHARE_FILE, Context.MODE_PRIVATE);
CLASS_SIMPLENAME = defValue.getClass().getSimpleName();
if("String".equals(CLASS_SIMPLENAME)){
return sharedPreferences.getString(key, (String) defValue);
}
if("Integer".equals(CLASS_SIMPLENAME)){
return sharedPreferences.getInt(key, (Integer) defValue);
}
if("Boolean".equals(CLASS_SIMPLENAME)){
return sharedPreferences.getBoolean(key, (Boolean) defValue);
}
if("Float".equals(CLASS_SIMPLENAME)){
return sharedPreferences.getFloat(key, (Float) defValue);
}
if("Long".equals(CLASS_SIMPLENAME)){
return sharedPreferences.getLong(key, (Long) defValue);
}
return null;
}
/**
* 不同数据类型缓存设置(指定缓存文件名称)<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key1", "caijin")<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key2", 10)<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key3", true)<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key4", 100L)<br/>
* AppSharePreferenceMgr.setParam(this, "userId", "key5", 1.1f)<br/>
* @param context 上下文对象
* @param fileKey 缓存文件名称(唯一标识)
* @param key 设置KEY
* @param object 设置VALUE
*/
public static Object getParam(Context context, String fileKey, String key, Object defValue){
sharedPreferences = context.getSharedPreferences(fileKey, Context.MODE_PRIVATE);
CLASS_SIMPLENAME = defValue.getClass().getSimpleName();
if("String".equals(CLASS_SIMPLENAME)){
return sharedPreferences.getString(key, (String) defValue);
}
if("Integer".equals(CLASS_SIMPLENAME)){
return sharedPreferences.getInt(key, (Integer) defValue);
}
if("Boolean".equals(CLASS_SIMPLENAME)){
return sharedPreferences.getBoolean(key, (Boolean) defValue);
}
if("Float".equals(CLASS_SIMPLENAME)){
return sharedPreferences.getFloat(key, (Float) defValue);
}
if("Long".equals(CLASS_SIMPLENAME)){
return sharedPreferences.getLong(key, (Long) defValue);
}
return null;
}
}
有代码洁癖的可以使用apply来替换掉commit,apply是异步的,感兴趣的可以了解一下
最后附上所有工具类的下载链接:
http://download.csdn.net/detail/u014727709/9697759
转载自:http://blog.csdn.net/u014727709/article/details/53390514
欢迎start,欢迎评论,欢迎指正