zoukankan      html  css  js  c++  java
  • Android 开发工具类 09_SPUtils

    SharedPreferences 辅助类:

    1、保存在手机里面的文件名;

    2、保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法;

    3、得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值;

    4、移除某个 key 值已经对应的值;

    5、清除所有数据;

    6、查询某个 key 是否已经存在;

    7、返回所有的键值对;

    8、创建一个解决 SharedPreferencesCompat.apply 方法的一个兼容类;

      1 import java.lang.reflect.InvocationTargetException;
      2 import java.lang.reflect.Method;
      3 import java.util.Map;
      4 
      5 import android.content.Context;
      6 import android.content.SharedPreferences;
      7 
      8 public class SPUtils
      9 {
     10     public SPUtils()
     11     {
     12         /* cannot be instantiated */
     13         throw new UnsupportedOperationException("cannot be instantiated");
     14     }
     15 
     16     /**
     17      * 保存在手机里面的文件名
     18      */
     19     public static final String FILE_NAME = "share_data";
     20 
     21     /**
     22      * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
     23      * 
     24      * @param context
     25      * @param key
     26      * @param object
     27      */
     28     public static void put(Context context, String key, Object object)
     29     {
     30 
     31         SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
     32                 Context.MODE_PRIVATE);
     33         SharedPreferences.Editor editor = sp.edit();
     34 
     35         if (object instanceof String)
     36         {
     37             editor.putString(key, (String) object);
     38         } else if (object instanceof Integer)
     39         {
     40             editor.putInt(key, (Integer) object);
     41         } else if (object instanceof Boolean)
     42         {
     43             editor.putBoolean(key, (Boolean) object);
     44         } else if (object instanceof Float)
     45         {
     46             editor.putFloat(key, (Float) object);
     47         } else if (object instanceof Long)
     48         {
     49             editor.putLong(key, (Long) object);
     50         } else
     51         {
     52             editor.putString(key, object.toString());
     53         }
     54 
     55         SharedPreferencesCompat.apply(editor);
     56     }
     57 
     58     /**
     59      * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
     60      * 
     61      * @param context
     62      * @param key
     63      * @param defaultObject
     64      * @return
     65      */
     66     public static Object get(Context context, String key, Object defaultObject)
     67     {
     68         SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
     69                 Context.MODE_PRIVATE);
     70 
     71         if (defaultObject instanceof String)
     72         {
     73             return sp.getString(key, (String) defaultObject);
     74         } else if (defaultObject instanceof Integer)
     75         {
     76             return sp.getInt(key, (Integer) defaultObject);
     77         } else if (defaultObject instanceof Boolean)
     78         {
     79             return sp.getBoolean(key, (Boolean) defaultObject);
     80         } else if (defaultObject instanceof Float)
     81         {
     82             return sp.getFloat(key, (Float) defaultObject);
     83         } else if (defaultObject instanceof Long)
     84         {
     85             return sp.getLong(key, (Long) defaultObject);
     86         }
     87 
     88         return null;
     89     }
     90 
     91     /**
     92      * 移除某个 key 值已经对应的值
     93      * 
     94      * @param context
     95      * @param key
     96      */
     97     public static void remove(Context context, String key)
     98     {
     99         SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
    100                 Context.MODE_PRIVATE);
    101         SharedPreferences.Editor editor = sp.edit();
    102         editor.remove(key);
    103         SharedPreferencesCompat.apply(editor);
    104     }
    105 
    106     /**
    107      * 清除所有数据
    108      * 
    109      * @param context
    110      */
    111     public static void clear(Context context)
    112     {
    113         SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
    114                 Context.MODE_PRIVATE);
    115         SharedPreferences.Editor editor = sp.edit();
    116         editor.clear();
    117         SharedPreferencesCompat.apply(editor);
    118     }
    119 
    120     /**
    121      * 查询某个 key 是否已经存在
    122      * 
    123      * @param context
    124      * @param key
    125      * @return
    126      */
    127     public static boolean contains(Context context, String key)
    128     {
    129         SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
    130                 Context.MODE_PRIVATE);
    131         return sp.contains(key);
    132     }
    133 
    134     /**
    135      * 返回所有的键值对
    136      * 
    137      * @param context
    138      * @return
    139      */
    140     public static Map<String, ?> getAll(Context context)
    141     {
    142         SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
    143                 Context.MODE_PRIVATE);
    144         return sp.getAll();
    145     }
    146 
    147     /**
    148      * 创建一个解决 SharedPreferencesCompat.apply 方法的一个兼容类
    149      * 
    150      * @author zhy
    151      * 
    152      */
    153     private static class SharedPreferencesCompat
    154     {
    155         private static final Method sApplyMethod = findApplyMethod();
    156 
    157         /**
    158          * 反射查找 apply 的方法
    159          * 
    160          * @return
    161          */
    162         @SuppressWarnings({ "unchecked", "rawtypes" })
    163         private static Method findApplyMethod()
    164         {
    165             try
    166             {
    167                 Class clz = SharedPreferences.Editor.class;
    168                 return clz.getMethod("apply");
    169             } catch (NoSuchMethodException e)
    170             {
    171             }
    172 
    173             return null;
    174         }
    175 
    176         /**
    177          * 如果找到则使用 apply 执行,否则使用 commit
    178          * 
    179          * @param editor
    180          */
    181         public static void apply(SharedPreferences.Editor editor)
    182         {
    183             try
    184             {
    185                 if (sApplyMethod != null)
    186                 {
    187                     sApplyMethod.invoke(editor);
    188                     return;
    189                 }
    190             } catch (IllegalArgumentException e)
    191             {
    192             } catch (IllegalAccessException e)
    193             {
    194             } catch (InvocationTargetException e)
    195             {
    196             }
    197             editor.commit();
    198         }
    199     }
    200 
    201 }
  • 相关阅读:
    bwapp之xss(blog)
    渗透测试平台bwapp简单介绍及安装
    用glob()函数返回目录下的子文件以及子目录
    常见编码解码脚本
    生成以指定字符为开头的md5值(6位数字)
    从1到n的阶乘的和(python)
    python循环解码base64
    BASE64编码原理分析脚本实现及逆向案例
    史上最完整的MySQL注入
    初探APT攻击
  • 原文地址:https://www.cnblogs.com/renzimu/p/4535706.html
Copyright © 2011-2022 走看看