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<String, ?> getAll(Context context) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
Map<String, ?> 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);
} }

SPUtils的更多相关文章

  1. SharedPreferences封装类SPUtils

    import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.uti ...

  2. Android常用的工具类SharedPreferences封装类SPUtils

    package com.zhy.utils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect. ...

  3. The Engine Document of JustWeEngine

    JustWeEngine - Android FrameWork An easy open source Android Native Game FrameWork. Github Game core ...

  4. JustWeTools - 自定义控件集

    JustWeTools - Some useful tools 项目地址 JustWe 现在有哪些模块? View自定义控件 PaintView画图工具(包含重构压感新版) CodeView代码编辑 ...

  5. SharePrecences--(json+sharePrecences)存list 或对象

    利用Gson和SharePreference存储结构化数据 具体的步骤 这个假设有三个User对象生成一个ArrayList<User>: User user1 = new User(&q ...

  6. JustWeEngine - 轻量级游戏框架

    JustWeEngine - 轻量级游戏框架 An easy open source Android game engine. Github地址 引擎核心类流程图 使用方法 引入Engine作为Lib ...

  7. Android引导指示层的制作 (ViewStub + SharePreference)

    引导指示界面是个什么鬼东西?一张图即明了:

  8. 59.Android开源项目及库 (转)

    转载 : https://github.com/Tim9Liu9/TimLiu-Android?hmsr=toutiao.io&utm_medium=toutiao.io&utm_so ...

  9. Android快速开发系列 10个常用工具类

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38965311,本文出自[张鸿洋的博客] 打开大家手上的项目,基本都会有一大批的辅 ...

随机推荐

  1. poj3656

    题解: 二分图最大匹配 根据三角形不等式 直接上最大匹配即可 注意编圈取相反数 代码: #include<iostream> #include<algorithm> #incl ...

  2. LeetCode OJ:Longest Increasing Subsequence(最长递增序列)

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  3. Jxl的API概述(转)

    一.Jxl的API Jxl的API主要有三个包,jxl,jxl.format,jxl.write.如果单独的分析API,可能对于更明确的了解此API没有太多的帮助,我们还是从Excel文件的层次来剥离 ...

  4. Mimiktaz抓取本机密码

    Mimiktaz2.0以后的版本只需要两条命令即可实现密码的抓取 mimikatz # privilege::debug mimikatz # sekurlsa::logonpasswords

  5. live555源码分析

    live555源代码下载(VC6工程):http://download.csdn.net/detail/leixiaohua1020/6374387 liveMedia 项目(http://www.l ...

  6. 手游服务端框架之GM金手指的设计

    玩过单机游戏的朋友,应该对金山游侠这个软件很熟悉把.当初我经常嫌刷怪升级非常辛苦,很多时候都是直接用金山游侠来修改游戏的经验或者等级内存,直接把角色调得很牛逼. 游戏开发也非常需要这些可以修改玩家数据 ...

  7. Android Studio3.0 新特性 ~ New Features in Android Studio Preview (译文)

    原文地址:https://developer.android.google.cn/studio/preview/features/index.html 最新Android Studio版本是Andro ...

  8. ios6:新特征介绍

    下面我们分别来看看这些特征的一些介绍: 1.地图     iOS6抛弃了一直用的google map,而使用了自家的地图服务.相应地,MapKit框架也自然变成和Apple自家的地图服务绑定了.随之而 ...

  9. 有关linux中,<math.h>的调用方法

    h{font-weight:bold;color:green;font-size:105%} p{font-size:100%} linux下C语言程序中,若要用到math.h中的函数(如:sin() ...

  10. BloomFilter ——大规模数据处理利器

    BloomFilter——大规模数据处理利器 Bloom Filter是由Bloom在1970年提出的一种多哈希函数映射的快速查找算法.通常应用在一些需要快速判断某个元素是否属于集合,但是并不严格要求 ...