zoukankan      html  css  js  c++  java
  • Android常用的工具类SharedPreferences封装类SPUtils

      1 package com.zhy.utils;
      2 
      3 import java.lang.reflect.InvocationTargetException;
      4 import java.lang.reflect.Method;
      5 import java.util.Map;
      6 
      7 import android.content.Context;
      8 import android.content.SharedPreferences;
      9 
     10 public class SPUtils
     11 {
     12     /**
     13      * 保存在手机里面的文件名
     14      */
     15     public static final String FILE_NAME = "share_data";
     16 
     17     /**
     18      * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
     19      * 
     20      * @param context
     21      * @param key
     22      * @param object
     23      */
     24     public static void put(Context context, String key, Object object)
     25     {
     26 
     27         SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
     28                 Context.MODE_PRIVATE);
     29         SharedPreferences.Editor editor = sp.edit();
     30 
     31         if (object instanceof String)
     32         {
     33             editor.putString(key, (String) object);
     34         } else if (object instanceof Integer)
     35         {
     36             editor.putInt(key, (Integer) object);
     37         } else if (object instanceof Boolean)
     38         {
     39             editor.putBoolean(key, (Boolean) object);
     40         } else if (object instanceof Float)
     41         {
     42             editor.putFloat(key, (Float) object);
     43         } else if (object instanceof Long)
     44         {
     45             editor.putLong(key, (Long) object);
     46         } else
     47         {
     48             editor.putString(key, object.toString());
     49         }
     50 
     51         SharedPreferencesCompat.apply(editor);
     52     }
     53 
     54     /**
     55      * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
     56      * 
     57      * @param context
     58      * @param key
     59      * @param defaultObject
     60      * @return
     61      */
     62     public static Object get(Context context, String key, Object defaultObject)
     63     {
     64         SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
     65                 Context.MODE_PRIVATE);
     66 
     67         if (defaultObject instanceof String)
     68         {
     69             return sp.getString(key, (String) defaultObject);
     70         } else if (defaultObject instanceof Integer)
     71         {
     72             return sp.getInt(key, (Integer) defaultObject);
     73         } else if (defaultObject instanceof Boolean)
     74         {
     75             return sp.getBoolean(key, (Boolean) defaultObject);
     76         } else if (defaultObject instanceof Float)
     77         {
     78             return sp.getFloat(key, (Float) defaultObject);
     79         } else if (defaultObject instanceof Long)
     80         {
     81             return sp.getLong(key, (Long) defaultObject);
     82         }
     83 
     84         return null;
     85     }
     86 
     87     /**
     88      * 移除某个key值已经对应的值
     89      * @param context
     90      * @param key
     91      */
     92     public static void remove(Context context, String key)
     93     {
     94         SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
     95                 Context.MODE_PRIVATE);
     96         SharedPreferences.Editor editor = sp.edit();
     97         editor.remove(key);
     98         SharedPreferencesCompat.apply(editor);
     99     }
    100 
    101     /**
    102      * 清除所有数据
    103      * @param context
    104      */
    105     public static void clear(Context context)
    106     {
    107         SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
    108                 Context.MODE_PRIVATE);
    109         SharedPreferences.Editor editor = sp.edit();
    110         editor.clear();
    111         SharedPreferencesCompat.apply(editor);
    112     }
    113 
    114     /**
    115      * 查询某个key是否已经存在
    116      * @param context
    117      * @param key
    118      * @return
    119      */
    120     public static boolean contains(Context context, String key)
    121     {
    122         SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
    123                 Context.MODE_PRIVATE);
    124         return sp.contains(key);
    125     }
    126 
    127     /**
    128      * 返回所有的键值对
    129      * 
    130      * @param context
    131      * @return
    132      */
    133     public static Map<String, ?> getAll(Context context)
    134     {
    135         SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
    136                 Context.MODE_PRIVATE);
    137         return sp.getAll();
    138     }
    139 
    140     /**
    141      * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
    142      * 
    143      * @author zhy
    144      * 
    145      */
    146     private static class SharedPreferencesCompat
    147     {
    148         private static final Method sApplyMethod = findApplyMethod();
    149 
    150         /**
    151          * 反射查找apply的方法
    152          * 
    153          * @return
    154          */
    155         @SuppressWarnings({ "unchecked", "rawtypes" })
    156         private static Method findApplyMethod()
    157         {
    158             try
    159             {
    160                 Class clz = SharedPreferences.Editor.class;
    161                 return clz.getMethod("apply");
    162             } catch (NoSuchMethodException e)
    163             {
    164             }
    165 
    166             return null;
    167         }
    168 
    169         /**
    170          * 如果找到则使用apply执行,否则使用commit
    171          * 
    172          * @param editor
    173          */
    174         public static void apply(SharedPreferences.Editor editor)
    175         {
    176             try
    177             {
    178                 if (sApplyMethod != null)
    179                 {
    180                     sApplyMethod.invoke(editor);
    181                     return;
    182                 }
    183             } catch (IllegalArgumentException e)
    184             {
    185             } catch (IllegalAccessException e)
    186             {
    187             } catch (InvocationTargetException e)
    188             {
    189             }
    190             editor.commit();
    191         }
    192     }
    193 
    194 }

    对SharedPreference的使用做了建议的封装,对外公布出put,get,remove,clear等等方法;

    注意一点,里面所有的commit操作使用了SharedPreferencesCompat.apply进行了替代,目的是尽可能的使用apply代替commit

    首先说下为什么,因为commit方法是同步的,并且我们很多时候的commit操作都是UI线程中,毕竟是IO操作,尽可能异步;

    所以我们使用apply进行替代,apply异步的进行写入;

    但是apply相当于commit来说是new API呢,为了更好的兼容,我们做了适配;

    SharedPreferencesCompat也可以给大家创建兼容类提供了一定的参考~~

  • 相关阅读:
    python 日期、时间戳转换
    判断任意数字是否为素数
    linux使用工具记录
    python日志记录-logging模块
    python特性、属性以及私有化
    python 装饰器、内部函数、闭包简单理解
    sql语句操作记录
    virtualBox使用nat模式下ssh连接
    git常用操作
    分布式CAP定理(转)
  • 原文地址:https://www.cnblogs.com/mlgm/p/7722647.html
Copyright © 2011-2022 走看看