SharedPreferences封装类SPUtils


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值