28569c7fd7bfd8f989992368d42b09c6783319cb.svn-base 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_MULTI_PROCESS);
  60. SharedPreferences.Editor editor = sp.edit();
  61. if (object instanceof String) {
  62. editor.putString(key, (String) object);
  63. } else if (object instanceof Integer) {
  64. editor.putInt(key, (Integer) object);
  65. } else if (object instanceof Boolean) {
  66. editor.putBoolean(key, (Boolean) object);
  67. } else if (object instanceof Float) {
  68. editor.putFloat(key, (Float) object);
  69. } else if (object instanceof Long) {
  70. editor.putLong(key, (Long) object);
  71. } else {
  72. editor.putString(key, object.toString());
  73. }
  74. SharedPreferencesCompat.apply(editor);
  75. }
  76. /**
  77. * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
  78. *
  79. * @param
  80. * @param key
  81. * @param defaultObject
  82. * @return
  83. */
  84. public static Object get(String key, Object defaultObject) {
  85. SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
  86. Context.MODE_PRIVATE);
  87. if (defaultObject instanceof String) {
  88. return sp.getString(key, (String) defaultObject);
  89. } else if (defaultObject instanceof Integer) {
  90. return sp.getInt(key, (Integer) defaultObject);
  91. } else if (defaultObject instanceof Boolean) {
  92. return sp.getBoolean(key, (Boolean) defaultObject);
  93. } else if (defaultObject instanceof Float) {
  94. return sp.getFloat(key, (Float) defaultObject);
  95. } else if (defaultObject instanceof Long) {
  96. return sp.getLong(key, (Long) defaultObject);
  97. }
  98. return null;
  99. }
  100. /**
  101. * 移除某个key值已经对应的值
  102. *
  103. * @param
  104. * @param key
  105. */
  106. public static void remove(String key) {
  107. SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
  108. Context.MODE_PRIVATE);
  109. SharedPreferences.Editor editor = sp.edit();
  110. editor.remove(key);
  111. SharedPreferencesCompat.apply(editor);
  112. }
  113. public static void logout() {
  114. remove("USER_PHONE");
  115. remove("USER_PWD");
  116. remove("USER_ID");
  117. remove("USER_TOKEN");
  118. remove("FlyPhoneType"); //fnc位置提示
  119. }
  120. /**
  121. * 清除所有数据
  122. * @param
  123. */
  124. // public static void clear()
  125. // {
  126. // SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
  127. // Context.MODE_PRIVATE);
  128. // SharedPreferences.Editor editor = sp.edit();
  129. // editor.clear();
  130. // SharedPreferencesCompat.apply(editor);
  131. //
  132. // }
  133. /**
  134. * 查询某个key是否已经存在
  135. *
  136. * @param
  137. * @param key
  138. * @return
  139. */
  140. public static boolean contains(String key) {
  141. SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
  142. Context.MODE_PRIVATE);
  143. return sp.contains(key);
  144. }
  145. /**
  146. * 返回所有的键值对
  147. *
  148. * @param
  149. * @return
  150. */
  151. public static Map<String, ?> getAll() {
  152. SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
  153. Context.MODE_PRIVATE);
  154. return sp.getAll();
  155. }
  156. /**
  157. * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
  158. *
  159. * @author zhy
  160. */
  161. private static class SharedPreferencesCompat {
  162. private static final Method sApplyMethod = findApplyMethod();
  163. /**
  164. * 反射查找apply的方法
  165. *
  166. * @return
  167. */
  168. @SuppressWarnings({"unchecked", "rawtypes"})
  169. private static Method findApplyMethod() {
  170. try {
  171. Class clz = SharedPreferences.Editor.class;
  172. return clz.getMethod("apply");
  173. } catch (NoSuchMethodException e) {
  174. }
  175. return null;
  176. }
  177. /**
  178. * 如果找到则使用apply执行,否则使用commit
  179. *
  180. * @param editor
  181. */
  182. public static void apply(SharedPreferences.Editor editor) {
  183. try {
  184. if (sApplyMethod != null) {
  185. sApplyMethod.invoke(editor);
  186. return;
  187. }
  188. } catch (IllegalArgumentException e) {
  189. } catch (IllegalAccessException e) {
  190. } catch (InvocationTargetException e) {
  191. }
  192. editor.commit();
  193. }
  194. }
  195. }