a13af4c7decefce826615038ca8b4cdef40fa9bf.svn-base 5.2 KB

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