9586dd809f9f02f5238a9120da0f6510bc65cc9d.svn-base 6.1 KB

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