博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SharedPreference工具类
阅读量:4322 次
发布时间:2019-06-06

本文共 2656 字,大约阅读时间需要 8 分钟。

public class SPUtils {    /**     * 保存在手机里的SP文件名     */    public static final String FILE_NAME = "my_sp";    /**     * 保存数据     */    public static void put(Context context, String key, Object obj) {        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);        SharedPreferences.Editor editor = sp.edit();        if (obj instanceof Boolean) {            editor.putBoolean(key, (Boolean) obj);        } else if (obj instanceof Float) {            editor.putFloat(key, (Float) obj);        } else if (obj instanceof Integer) {            editor.putInt(key, (Integer) obj);        } else if (obj instanceof Long) {            editor.putLong(key, (Long) obj);        } else {            editor.putString(key, (String) obj);        }        editor.commit();    }    /**     * 获取指定数据     */    public static Object get(Context context, String key, Object defaultObj) {        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);        if (defaultObj instanceof Boolean) {            return sp.getBoolean(key, (Boolean) defaultObj);        } else if (defaultObj instanceof Float) {            return sp.getFloat(key, (Float) defaultObj);        } else if (defaultObj instanceof Integer) {            return sp.getInt(key, (Integer) defaultObj);        } else if (defaultObj instanceof Long) {            return sp.getLong(key, (Long) defaultObj);        } else if (defaultObj instanceof String) {            return sp.getString(key, (String) defaultObj);        }        return null;    }    /**     * 删除指定数据     */    public static void remove(Context context, String key) {        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);        SharedPreferences.Editor editor = sp.edit();        editor.remove(key);        editor.commit();    }    /**     * 返回所有键值对     */    public static Map
getAll(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE); Map
map = sp.getAll(); return map; } /** * 删除所有数据 */ public static void clear(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.clear(); editor.commit(); } /** * 检查key对应的数据是否存在 */ public static boolean contains(Context context, String key) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE); return sp.contains(key); }}

 

转载于:https://www.cnblogs.com/yl-saber/p/7062030.html

你可能感兴趣的文章
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>
SAM4E单片机之旅——24、使用DSP库求向量数量积
查看>>
从远程库克隆库
查看>>
codeforces Unusual Product
查看>>
hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
查看>>
正则表达式的搜索和替换
查看>>
个人项目:WC
查看>>
地鼠的困境SSL1333 最大匹配
查看>>
flume+elasticsearch+kibana遇到的坑
查看>>
【MM系列】在SAP里查看数据的方法
查看>>
C#——winform
查看>>
CSS3 transform制作的漂亮的滚动式导航
查看>>
《小强升职记——时间管理故事书》读书笔记
查看>>
Alpha 冲刺(3/10)
查看>>
Kaldi中的Chain模型
查看>>
spring中的ResourceBundleMessageSource使用和测试示例
查看>>
css规范 - bem
查看>>
电梯调度程序的UI设计
查看>>
转自 zera php中extends和implements的区别
查看>>