Commit 9be45826697ce068c5852e9f00efe0d9c046f636
1 parent
c7efa41793
Exists in
master
避免版本升级实体类变化导致崩溃
Showing
2 changed files
with
12 additions
and
2 deletions
Show diff stats
app/src/main/java/com/hjx/parent/App.java
| 1 | package com.hjx.parent; | 1 | package com.hjx.parent; |
| 2 | 2 | ||
| 3 | import com.prws.common.CommonApplication; | 3 | import com.prws.common.CommonApplication; |
| 4 | import com.prws.common.bean.GradeAndSubject; | 4 | import com.prws.common.bean.GradeAndSubject; |
| 5 | import com.prws.common.utils.SharedPreferencesUtil; | 5 | import com.prws.common.utils.SharedPreferencesUtil; |
| 6 | import com.uuzuche.lib_zxing.activity.ZXingLibrary; | 6 | import com.uuzuche.lib_zxing.activity.ZXingLibrary; |
| 7 | 7 | ||
| 8 | import java.util.List; | 8 | import java.util.List; |
| 9 | 9 | ||
| 10 | public class App extends CommonApplication { | 10 | public class App extends CommonApplication { |
| 11 | private static App instance; | 11 | private static App instance; |
| 12 | 12 | ||
| 13 | public static App getInstance() { | 13 | public static App getInstance() { |
| 14 | return instance; | 14 | return instance; |
| 15 | } | 15 | } |
| 16 | 16 | ||
| 17 | private static String baiduToken; | 17 | private static String baiduToken; |
| 18 | 18 | ||
| 19 | public static void setBaiduToken(String baiduToken) { | 19 | public static void setBaiduToken(String baiduToken) { |
| 20 | App.baiduToken = baiduToken; | 20 | App.baiduToken = baiduToken; |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | public static String getBaiduToken() { | 23 | public static String getBaiduToken() { |
| 24 | return baiduToken; | 24 | return baiduToken; |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | private static List<GradeAndSubject> gradeAndSubjects; | 27 | private static List<GradeAndSubject> gradeAndSubjects; |
| 28 | 28 | ||
| 29 | public void setGradeAndSubjects(List<GradeAndSubject> gradeAndSubjects) { | 29 | public void setGradeAndSubjects(List<GradeAndSubject> gradeAndSubjects) { |
| 30 | App.gradeAndSubjects = gradeAndSubjects; | 30 | App.gradeAndSubjects = gradeAndSubjects; |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | public List<GradeAndSubject> getGradeAndSubjects() { | 33 | public List<GradeAndSubject> getGradeAndSubjects() { |
| 34 | return gradeAndSubjects; | 34 | return gradeAndSubjects; |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | @Override | 37 | @Override |
| 38 | public void onCreate() { | 38 | public void onCreate() { |
| 39 | super.onCreate(); | 39 | super.onCreate(); |
| 40 | this.instance = this; | 40 | this.instance = this; |
| 41 | SharedPreferencesUtil.getInstance(this, "config"); | 41 | SharedPreferencesUtil.getInstance(this, "config"); |
| 42 | SharedPreferencesUtil.checkVersion(this); | ||
| 42 | ZXingLibrary.initDisplayOpinion(this); | 43 | ZXingLibrary.initDisplayOpinion(this); |
| 43 | } | 44 | } |
| 44 | } | 45 | } |
| 45 | 46 |
libs/common/src/main/java/com/prws/common/utils/SharedPreferencesUtil.java
| 1 | package com.prws.common.utils; | 1 | package com.prws.common.utils; |
| 2 | 2 | ||
| 3 | import android.content.Context; | 3 | import android.content.Context; |
| 4 | import android.content.SharedPreferences; | 4 | import android.content.SharedPreferences; |
| 5 | import android.util.Log; | 5 | import android.util.Log; |
| 6 | 6 | ||
| 7 | import com.google.gson.Gson; | 7 | import com.google.gson.Gson; |
| 8 | import com.google.gson.JsonArray; | 8 | import com.google.gson.JsonArray; |
| 9 | import com.google.gson.JsonElement; | 9 | import com.google.gson.JsonElement; |
| 10 | import com.google.gson.JsonObject; | 10 | import com.google.gson.JsonObject; |
| 11 | import com.google.gson.JsonParser; | 11 | import com.google.gson.JsonParser; |
| 12 | 12 | ||
| 13 | import java.util.ArrayList; | 13 | import java.util.ArrayList; |
| 14 | import java.util.HashMap; | 14 | import java.util.HashMap; |
| 15 | import java.util.List; | 15 | import java.util.List; |
| 16 | import java.util.Map; | 16 | import java.util.Map; |
| 17 | import java.util.Set; | 17 | import java.util.Set; |
| 18 | 18 | ||
| 19 | /** | 19 | /** |
| 20 | * Description: This's SharedPreferencesUtil | 20 | * Description: This's SharedPreferencesUtil |
| 21 | * Time: 2018/7/10 15:05 | 21 | * Time: 2018/7/10 15:05 |
| 22 | * | 22 | * |
| 23 | * @author lishiting | 23 | * @author lishiting |
| 24 | */ | 24 | */ |
| 25 | public class SharedPreferencesUtil { | 25 | public class SharedPreferencesUtil { |
| 26 | private static SharedPreferencesUtil util; | 26 | private static SharedPreferencesUtil util; |
| 27 | private static SharedPreferences sp; | 27 | private static SharedPreferences sp; |
| 28 | 28 | ||
| 29 | private SharedPreferencesUtil(Context context, String name) { | 29 | private SharedPreferencesUtil(Context context, String name) { |
| 30 | sp = context.getSharedPreferences(name, Context.MODE_PRIVATE); | 30 | sp = context.getSharedPreferences(name, Context.MODE_PRIVATE); |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | /** | 33 | /** |
| 34 | * 初始化SharedPreferencesUtil,只需要初始化一次,建议在Application中初始化 | 34 | * 初始化SharedPreferencesUtil,只需要初始化一次,建议在Application中初始化 |
| 35 | * | 35 | * |
| 36 | * @param context 上下文对象 | 36 | * @param context 上下文对象 |
| 37 | * @param name SharedPreferences Name | 37 | * @param name SharedPreferences Name |
| 38 | */ | 38 | */ |
| 39 | public static void getInstance(Context context, String name) { | 39 | public static void getInstance(Context context, String name) { |
| 40 | if (util == null) { | 40 | if (util == null) { |
| 41 | util = new SharedPreferencesUtil(context, name); | 41 | util = new SharedPreferencesUtil(context, name); |
| 42 | } | 42 | } |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | /** | 45 | /** |
| 46 | * 保存数据到SharedPreferences | 46 | * 保存数据到SharedPreferences |
| 47 | * | 47 | * |
| 48 | * @param key 键 | 48 | * @param key 键 |
| 49 | * @param value 需要保存的数据 | 49 | * @param value 需要保存的数据 |
| 50 | * @return 保存结果 | 50 | * @return 保存结果 |
| 51 | */ | 51 | */ |
| 52 | public static boolean putData(String key, Object value) { | 52 | public static boolean putData(String key, Object value) { |
| 53 | boolean result; | 53 | boolean result; |
| 54 | SharedPreferences.Editor editor = sp.edit(); | 54 | SharedPreferences.Editor editor = sp.edit(); |
| 55 | String type = value.getClass().getSimpleName(); | 55 | String type = value.getClass().getSimpleName(); |
| 56 | try { | 56 | try { |
| 57 | switch (type) { | 57 | switch (type) { |
| 58 | case "Boolean": | 58 | case "Boolean": |
| 59 | editor.putBoolean(key, (Boolean) value); | 59 | editor.putBoolean(key, (Boolean) value); |
| 60 | break; | 60 | break; |
| 61 | case "Long": | 61 | case "Long": |
| 62 | editor.putLong(key, (Long) value); | 62 | editor.putLong(key, (Long) value); |
| 63 | break; | 63 | break; |
| 64 | case "Float": | 64 | case "Float": |
| 65 | editor.putFloat(key, (Float) value); | 65 | editor.putFloat(key, (Float) value); |
| 66 | break; | 66 | break; |
| 67 | case "String": | 67 | case "String": |
| 68 | editor.putString(key, (String) value); | 68 | editor.putString(key, (String) value); |
| 69 | break; | 69 | break; |
| 70 | case "Integer": | 70 | case "Integer": |
| 71 | editor.putInt(key, (Integer) value); | 71 | editor.putInt(key, (Integer) value); |
| 72 | break; | 72 | break; |
| 73 | default: | 73 | default: |
| 74 | Gson gson = new Gson(); | 74 | Gson gson = new Gson(); |
| 75 | String json = gson.toJson(value); | 75 | String json = gson.toJson(value); |
| 76 | editor.putString(key, json); | 76 | editor.putString(key, json); |
| 77 | break; | 77 | break; |
| 78 | } | 78 | } |
| 79 | result = true; | 79 | result = true; |
| 80 | } catch (Exception e) { | 80 | } catch (Exception e) { |
| 81 | result = false; | 81 | result = false; |
| 82 | e.printStackTrace(); | 82 | e.printStackTrace(); |
| 83 | } | 83 | } |
| 84 | editor.apply(); | 84 | editor.apply(); |
| 85 | return result; | 85 | return result; |
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | /** | 88 | /** |
| 89 | * 获取SharedPreferences中保存的数据 | 89 | * 获取SharedPreferences中保存的数据 |
| 90 | * | 90 | * |
| 91 | * @param key 键 | 91 | * @param key 键 |
| 92 | * @param defaultValue 获取失败默认值 | 92 | * @param defaultValue 获取失败默认值 |
| 93 | * @return 从SharedPreferences读取的数据 | 93 | * @return 从SharedPreferences读取的数据 |
| 94 | */ | 94 | */ |
| 95 | public static Object getData(String key, Object defaultValue) { | 95 | public static Object getData(String key, Object defaultValue) { |
| 96 | Object result; | 96 | Object result; |
| 97 | String type = defaultValue.getClass().getSimpleName(); | 97 | String type = defaultValue.getClass().getSimpleName(); |
| 98 | try { | 98 | try { |
| 99 | switch (type) { | 99 | switch (type) { |
| 100 | case "Boolean": | 100 | case "Boolean": |
| 101 | result = sp.getBoolean(key, (Boolean) defaultValue); | 101 | result = sp.getBoolean(key, (Boolean) defaultValue); |
| 102 | break; | 102 | break; |
| 103 | case "Long": | 103 | case "Long": |
| 104 | result = sp.getLong(key, (Long) defaultValue); | 104 | result = sp.getLong(key, (Long) defaultValue); |
| 105 | break; | 105 | break; |
| 106 | case "Float": | 106 | case "Float": |
| 107 | result = sp.getFloat(key, (Float) defaultValue); | 107 | result = sp.getFloat(key, (Float) defaultValue); |
| 108 | break; | 108 | break; |
| 109 | case "String": | 109 | case "String": |
| 110 | result = sp.getString(key, (String) defaultValue); | 110 | result = sp.getString(key, (String) defaultValue); |
| 111 | break; | 111 | break; |
| 112 | case "Integer": | 112 | case "Integer": |
| 113 | result = sp.getInt(key, (Integer) defaultValue); | 113 | result = sp.getInt(key, (Integer) defaultValue); |
| 114 | break; | 114 | break; |
| 115 | default: | 115 | default: |
| 116 | Gson gson = new Gson(); | 116 | Gson gson = new Gson(); |
| 117 | String json = sp.getString(key, ""); | 117 | String json = sp.getString(key, ""); |
| 118 | if (!json.equals("") && json.length() > 0) { | 118 | if (!json.equals("") && json.length() > 0) { |
| 119 | result = gson.fromJson(json, defaultValue.getClass()); | 119 | result = gson.fromJson(json, defaultValue.getClass()); |
| 120 | } else { | 120 | } else { |
| 121 | result = defaultValue; | 121 | result = defaultValue; |
| 122 | } | 122 | } |
| 123 | break; | 123 | break; |
| 124 | } | 124 | } |
| 125 | } catch (Exception e) { | 125 | } catch (Exception e) { |
| 126 | result = null; | 126 | result = null; |
| 127 | e.printStackTrace(); | 127 | e.printStackTrace(); |
| 128 | } | 128 | } |
| 129 | return result; | 129 | return result; |
| 130 | } | 130 | } |
| 131 | 131 | ||
| 132 | /** | 132 | /** |
| 133 | * 用于保存集合 | 133 | * 用于保存集合 |
| 134 | * | 134 | * |
| 135 | * @param key key | 135 | * @param key key |
| 136 | * @param list 集合数据 | 136 | * @param list 集合数据 |
| 137 | * @return 保存结果 | 137 | * @return 保存结果 |
| 138 | */ | 138 | */ |
| 139 | public static <T> boolean putListData(String key, List<T> list) { | 139 | public static <T> boolean putListData(String key, List<T> list) { |
| 140 | boolean result; | 140 | boolean result; |
| 141 | String type = list.get(0).getClass().getSimpleName(); | 141 | String type = list.get(0).getClass().getSimpleName(); |
| 142 | SharedPreferences.Editor editor = sp.edit(); | 142 | SharedPreferences.Editor editor = sp.edit(); |
| 143 | JsonArray array = new JsonArray(); | 143 | JsonArray array = new JsonArray(); |
| 144 | try { | 144 | try { |
| 145 | switch (type) { | 145 | switch (type) { |
| 146 | case "Boolean": | 146 | case "Boolean": |
| 147 | for (int i = 0; i < list.size(); i++) { | 147 | for (int i = 0; i < list.size(); i++) { |
| 148 | array.add((Boolean) list.get(i)); | 148 | array.add((Boolean) list.get(i)); |
| 149 | } | 149 | } |
| 150 | break; | 150 | break; |
| 151 | case "Long": | 151 | case "Long": |
| 152 | for (int i = 0; i < list.size(); i++) { | 152 | for (int i = 0; i < list.size(); i++) { |
| 153 | array.add((Long) list.get(i)); | 153 | array.add((Long) list.get(i)); |
| 154 | } | 154 | } |
| 155 | break; | 155 | break; |
| 156 | case "Float": | 156 | case "Float": |
| 157 | for (int i = 0; i < list.size(); i++) { | 157 | for (int i = 0; i < list.size(); i++) { |
| 158 | array.add((Float) list.get(i)); | 158 | array.add((Float) list.get(i)); |
| 159 | } | 159 | } |
| 160 | break; | 160 | break; |
| 161 | case "String": | 161 | case "String": |
| 162 | for (int i = 0; i < list.size(); i++) { | 162 | for (int i = 0; i < list.size(); i++) { |
| 163 | array.add((String) list.get(i)); | 163 | array.add((String) list.get(i)); |
| 164 | } | 164 | } |
| 165 | break; | 165 | break; |
| 166 | case "Integer": | 166 | case "Integer": |
| 167 | for (int i = 0; i < list.size(); i++) { | 167 | for (int i = 0; i < list.size(); i++) { |
| 168 | array.add((Integer) list.get(i)); | 168 | array.add((Integer) list.get(i)); |
| 169 | } | 169 | } |
| 170 | break; | 170 | break; |
| 171 | default: | 171 | default: |
| 172 | Gson gson = new Gson(); | 172 | Gson gson = new Gson(); |
| 173 | for (int i = 0; i < list.size(); i++) { | 173 | for (int i = 0; i < list.size(); i++) { |
| 174 | JsonElement obj = gson.toJsonTree(list.get(i)); | 174 | JsonElement obj = gson.toJsonTree(list.get(i)); |
| 175 | array.add(obj); | 175 | array.add(obj); |
| 176 | } | 176 | } |
| 177 | break; | 177 | break; |
| 178 | } | 178 | } |
| 179 | editor.putString(key, array.toString()); | 179 | editor.putString(key, array.toString()); |
| 180 | result = true; | 180 | result = true; |
| 181 | } catch (Exception e) { | 181 | } catch (Exception e) { |
| 182 | result = false; | 182 | result = false; |
| 183 | e.printStackTrace(); | 183 | e.printStackTrace(); |
| 184 | } | 184 | } |
| 185 | editor.apply(); | 185 | editor.apply(); |
| 186 | return result; | 186 | return result; |
| 187 | } | 187 | } |
| 188 | 188 | ||
| 189 | /** | 189 | /** |
| 190 | * 获取保存的List | 190 | * 获取保存的List |
| 191 | * | 191 | * |
| 192 | * @param key key | 192 | * @param key key |
| 193 | * @return 对应的Lis集合 | 193 | * @return 对应的Lis集合 |
| 194 | */ | 194 | */ |
| 195 | public static <T> List<T> getListData(String key, Class<T> cls) { | 195 | public static <T> List<T> getListData(String key, Class<T> cls) { |
| 196 | List<T> list = new ArrayList<>(); | 196 | List<T> list = new ArrayList<>(); |
| 197 | String json = sp.getString(key, ""); | 197 | String json = sp.getString(key, ""); |
| 198 | if (!json.equals("") && json.length() > 0) { | 198 | if (!json.equals("") && json.length() > 0) { |
| 199 | Gson gson = new Gson(); | 199 | Gson gson = new Gson(); |
| 200 | JsonArray array = new JsonParser().parse(json).getAsJsonArray(); | 200 | JsonArray array = new JsonParser().parse(json).getAsJsonArray(); |
| 201 | for (JsonElement elem : array) { | 201 | for (JsonElement elem : array) { |
| 202 | list.add(gson.fromJson(elem, cls)); | 202 | list.add(gson.fromJson(elem, cls)); |
| 203 | } | 203 | } |
| 204 | } | 204 | } |
| 205 | return list; | 205 | return list; |
| 206 | } | 206 | } |
| 207 | 207 | ||
| 208 | /** | 208 | /** |
| 209 | * 获删除数据 | 209 | * 获删除数据 |
| 210 | * | 210 | * |
| 211 | * @param key key | 211 | * @param key key |
| 212 | */ | 212 | */ |
| 213 | public static void delData(String key) { | 213 | public static void delData(String key) { |
| 214 | SharedPreferences.Editor editor = sp.edit(); | 214 | SharedPreferences.Editor editor = sp.edit(); |
| 215 | editor.remove(key); | 215 | editor.remove(key); |
| 216 | editor.apply(); | 216 | editor.apply(); |
| 217 | } | 217 | } |
| 218 | 218 | ||
| 219 | 219 | ||
| 220 | /** | 220 | /** |
| 221 | * 用于保存集合 | 221 | * 用于保存集合 |
| 222 | * | 222 | * |
| 223 | * @param key key | 223 | * @param key key |
| 224 | * @param map map数据 | 224 | * @param map map数据 |
| 225 | * @return 保存结果 | 225 | * @return 保存结果 |
| 226 | */ | 226 | */ |
| 227 | public static <K, V> boolean putHashMapData(String key, Map<K, V> map) { | 227 | public static <K, V> boolean putHashMapData(String key, Map<K, V> map) { |
| 228 | boolean result; | 228 | boolean result; |
| 229 | SharedPreferences.Editor editor = sp.edit(); | 229 | SharedPreferences.Editor editor = sp.edit(); |
| 230 | try { | 230 | try { |
| 231 | Gson gson = new Gson(); | 231 | Gson gson = new Gson(); |
| 232 | String json = gson.toJson(map); | 232 | String json = gson.toJson(map); |
| 233 | editor.putString(key, json); | 233 | editor.putString(key, json); |
| 234 | result = true; | 234 | result = true; |
| 235 | } catch (Exception e) { | 235 | } catch (Exception e) { |
| 236 | result = false; | 236 | result = false; |
| 237 | e.printStackTrace(); | 237 | e.printStackTrace(); |
| 238 | } | 238 | } |
| 239 | editor.apply(); | 239 | editor.apply(); |
| 240 | return result; | 240 | return result; |
| 241 | } | 241 | } |
| 242 | 242 | ||
| 243 | /** | 243 | /** |
| 244 | * 用于保存集合 | 244 | * 用于保存集合 |
| 245 | * | 245 | * |
| 246 | * @param key key | 246 | * @param key key |
| 247 | * @return HashMap | 247 | * @return HashMap |
| 248 | */ | 248 | */ |
| 249 | public static <V> HashMap<String, V> getHashMapData(String key, Class<V> clsV) { | 249 | public static <V> HashMap<String, V> getHashMapData(String key, Class<V> clsV) { |
| 250 | String json = sp.getString(key, ""); | 250 | String json = sp.getString(key, ""); |
| 251 | HashMap<String, V> map = new HashMap<>(); | 251 | HashMap<String, V> map = new HashMap<>(); |
| 252 | Gson gson = new Gson(); | 252 | Gson gson = new Gson(); |
| 253 | JsonObject obj = new JsonParser().parse(json).getAsJsonObject(); | 253 | JsonObject obj = new JsonParser().parse(json).getAsJsonObject(); |
| 254 | Set<Map.Entry<String, JsonElement>> entrySet = obj.entrySet(); | 254 | Set<Map.Entry<String, JsonElement>> entrySet = obj.entrySet(); |
| 255 | for (Map.Entry<String, JsonElement> entry : entrySet) { | 255 | for (Map.Entry<String, JsonElement> entry : entrySet) { |
| 256 | String entryKey = entry.getKey(); | 256 | String entryKey = entry.getKey(); |
| 257 | JsonObject value = (JsonObject) entry.getValue(); | 257 | JsonObject value = (JsonObject) entry.getValue(); |
| 258 | map.put(entryKey, gson.fromJson(value, clsV)); | 258 | map.put(entryKey, gson.fromJson(value, clsV)); |
| 259 | } | 259 | } |
| 260 | Log.e("SharedPreferencesUtil", obj.toString()); | 260 | Log.e("SharedPreferencesUtil", obj.toString()); |
| 261 | return map; | 261 | return map; |
| 262 | 262 | ||
| 263 | } | 263 | } |
| 264 | 264 | ||
| 265 | public static void clear(Context context) { | 265 | public static void clear(Context context) { |
| 266 | SharedPreferences preferences = context.getSharedPreferences("config", Context.MODE_PRIVATE); | 266 | SharedPreferences.Editor editor = sp.edit(); |
| 267 | SharedPreferences.Editor editor = preferences.edit(); | ||
| 268 | editor.clear(); | 267 | editor.clear(); |
| 269 | editor.commit(); | 268 | editor.commit(); |
| 270 | } | 269 | } |
| 271 | 270 | ||
| 271 | private static final int myVersion = 2; | ||
| 272 | public static void checkVersion(Context context) { | ||
| 273 | if (sp.getInt("myVersion", 0) == myVersion) { | ||
| 274 | return; | ||
| 275 | } | ||
| 276 | clear(context); | ||
| 277 | SharedPreferences.Editor editor = sp.edit(); | ||
| 278 | editor.putInt("myVersion", myVersion); | ||
| 279 | editor.apply(); | ||
| 280 | } | ||
| 272 | } | 281 | } |