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也可以給大家創建兼容類提供了一定的參考~~