import android.content.Context;
import android.content.SharedPreferences;
import java.util.HashSet;
import java.util.Set;
/**
* SharePreferences 工具类
* 如:保存 int、 String、 boolean、 float、long、 Set < String >,
* 获取int、 String、 boolean、 float、long、set,
* 清除当前xml 某一个key对应的值、所有值
*
*/
public class SharePreferencesUtils {
/**
* 保存 int
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要保存的 key
* @param value 要保存的 value
*/
public static void saveInt(Context context, String xmlName, String key, int value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.apply();
}
/**
* 保存 String
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要保存的 key
* @param value 要保存的 value
*/
public static void saveString(Context context, String xmlName, String key, String value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.apply();
}
/**
* 保存 boolean
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要保存的 key
* @param value 要保存的 value
*/
public static void saveBoolean(Context context, String xmlName, String key, boolean value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.apply();
}
/**
* 保存 float
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要保存的 key
* @param value 要保存的 value
*/
public static void saveFloat(Context context, String xmlName, String key, float value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putFloat(key, value);
editor.apply();
}
/**
* 保存 long
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要保存的 key
* @param value 要保存的 value
*/
public static void saveLong(Context context, String xmlName, String key, long value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong(key, value);
editor.apply();
}
/**
* 保存 Set < String >
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要保存的 key
* @param value 要保存的 value
*/
public static void saveSet(Context context, String xmlName, String key, Set<String> value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putStringSet(key, value);
editor.apply();
}
/**
* 获取set
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要获取的 key
*/
public static Set<String> getSet(Context context, String xmlName, String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
return sharedPreferences.getStringSet(key, new HashSet<String>());
}
/**
* 清除当前xml 某一个key对应的值
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要清除的key
*/
public static void clearOne(Context context, String xmlName, String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(key);
editor.apply();
}
/**
* 清除xml中所有的值
*
* @param context 上下文环境
* @param xmlName xml文件名
*/
public static void clearAll(Context context, String xmlName) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
}
/**
* 获取int 值
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要获取的key
* @param defaultValue 默认值
*/
public static int getInt(Context context, String xmlName, String key, int defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
int value = sharedPreferences.getInt(key, defaultValue);
return value;
}
/**
* 获取 String 值
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要获取的key
* @param defaultValue 默认值
*/
public static String getString(Context context, String xmlName, String key, String defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
return sharedPreferences.getString(key, defaultValue);
}
/**
* 获取Boolean 值
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要获取的key
* @param defaultValue 默认值
* @return
*/
public static boolean getBoolean(Context context, String xmlName, String key, boolean defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
return sharedPreferences.getBoolean(key, defaultValue);
}
/**
* 获取long 值
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要获取的key
* @param defaultValue 默认值
*/
public static long getLong(Context context, String xmlName, String key, long defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
return sharedPreferences.getLong(key, defaultValue);
}
/**
* 获取float 值
*
* @param context 上下文环境
* @param xmlName xml文件名
* @param key 要获取的key
* @param defaultValue 默认值
*/
public static float getFloat(Context context, String xmlName, String key, float defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(xmlName, Context.MODE_PRIVATE);
return sharedPreferences.getFloat(key, defaultValue);
}
}
[Android][工具类]SharePreferencesUtils
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 在任何APP开发中,日期和时间是无处不在的,例如QQ、微信,每条信息都会显示发送时间,还有空间、朋友圈每一条...
- [TOC] 前言 Android SDK原生 API中,有一些常用的工具类,运用得当可以省事省力省时,何况还是An...
- 1. IMEI IMEI(International Mobile Equipment Identity)是国际移...
- 前言 相信大部分仁兄在使用系统Toast的时候,都感觉不太尽如人意,因为系统Toast显示的位置比较固定,并且字体...