public class SharedPreUtil {
private static SharedPreferences mSp = null;
//缓存的config_sp.xml文件
private static final String CONFIG_SP ="config_sp";
private static SharedPreferences getPreferences(Context context) {
if(mSp == null) {
mSp=context.getSharedPreferences(CONFIG_SP, Context.MODE_PRIVATE);
}
return mSp;
}
/***************************boolean类型****************************************/
public static void putBoolean(Context context,String key, boolean value) {
SharedPreferences sp=getPreferences(context);
sp.edit().putBoolean(key, value).commit();
}
public static boolean getBoolean(Context context,String key) {
SharedPreferences sp=getPreferences(context);
return sp.getBoolean(key, false);
}
public static boolean getBoolean(Context context,String key,boolean defvalue) {
SharedPreferences sp=getPreferences(context);
return sp.getBoolean(key,defvalue);
}
/******************************************************************************/
/***************************String类型******************************************/
public static void putString(Context context,String key,String value) {
SharedPreferences sp=getPreferences(context);
sp.edit().putString(key, value).commit();
}
public static String getString(Context context,String key) {
SharedPreferences sp=getPreferences(context);
return sp.getString(key,null);
}
public static String getString(Context context,String key,String defvalue) {
SharedPreferences sp=getPreferences(context);
return sp.getString(key,defvalue);
}
/******************************************************************************/
/***************************int类型******************************************/
public static void putInt(Context context,String key,int value) {
SharedPreferences sp=getPreferences(context);
sp.edit().putInt(key, value).commit();
}
public static int getInt(Context context,String key,int defvalue) {
SharedPreferences sp=getPreferences(context);
return sp.getInt(key,defvalue);
}
/******************************************************************************/
/**
* 删除一个节点
*/
public static void remove(Context context, String key) {
SharedPreferences sp = getPreferences(context);
sp.edit().remove(key).commit();
}
/**
* 清除所有数据
*/
public static void clear(Context context) {
SharedPreferences sp = getPreferences(context);
sp.edit().clear();
}
}
Android-----SharedPreences工具类
最新推荐文章于 2024-10-15 21:09:19 发布