4723cf310e5a192e28af42775f928274e586abb6.svn-base 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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("USER_ROLEID");
  119. remove("USER_FACTORY_ID");
  120. remove("FlyPhoneType"); //fnc位置提示
  121. }
  122. /**
  123. * 清除所有数据
  124. * @param
  125. */
  126. // public static void clear()
  127. // {
  128. // SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
  129. // Context.MODE_PRIVATE);
  130. // SharedPreferences.Editor editor = sp.edit();
  131. // editor.clear();
  132. // SharedPreferencesCompat.apply(editor);
  133. //
  134. // }
  135. /**
  136. * 查询某个key是否已经存在
  137. *
  138. * @param
  139. * @param key
  140. * @return
  141. */
  142. public static boolean contains(String key) {
  143. SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
  144. Context.MODE_PRIVATE);
  145. return sp.contains(key);
  146. }
  147. /**
  148. * 返回所有的键值对
  149. *
  150. * @param
  151. * @return
  152. */
  153. public static Map<String, ?> getAll() {
  154. SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
  155. Context.MODE_PRIVATE);
  156. return sp.getAll();
  157. }
  158. /**
  159. * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
  160. *
  161. * @author zhy
  162. */
  163. private static class SharedPreferencesCompat {
  164. private static final Method sApplyMethod = findApplyMethod();
  165. /**
  166. * 反射查找apply的方法
  167. *
  168. * @return
  169. */
  170. @SuppressWarnings({"unchecked", "rawtypes"})
  171. private static Method findApplyMethod() {
  172. try {
  173. Class clz = SharedPreferences.Editor.class;
  174. return clz.getMethod("apply");
  175. } catch (NoSuchMethodException e) {
  176. }
  177. return null;
  178. }
  179. /**
  180. * 如果找到则使用apply执行,否则使用commit
  181. *
  182. * @param editor
  183. */
  184. public static void apply(SharedPreferences.Editor editor) {
  185. try {
  186. if (sApplyMethod != null) {
  187. sApplyMethod.invoke(editor);
  188. return;
  189. }
  190. } catch (IllegalArgumentException e) {
  191. } catch (IllegalAccessException e) {
  192. } catch (InvocationTargetException e) {
  193. }
  194. editor.commit();
  195. }
  196. }
  197. }