package com.lfk.justwetools.Utils; import android.content.Context; import android.content.SharedPreferences; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; /** * 简化Sp存储的工具类 * * @author liufengkai */ public class SpUtils { public SpUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** * Sp文件名 */ public static final String FILE_NAME = "share_data"; /** * 保存数据的方法,添加具体数据类型 * * @param context * @param key * @param object */ public static void put(Context context, String key, Object object) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); // 判断数据类型 if (object instanceof String) { editor.putString(key, (String) object); } else if (object instanceof Integer) { editor.putInt(key, (Integer) object); } else if (object instanceof Boolean) { editor.putBoolean(key, (Boolean) object); } else if (object instanceof Float) { editor.putFloat(key, (Float) object); } else if (object instanceof Long) { editor.putLong(key, (Long) object); } else { editor.putString(key, object.toString()); } SharedPreferencesCompat.apply(editor); } /** * 获取保存数据的方法,添加具体数据类型 * * @param context * @param key * @param defaultObject * @return object */ public static Object get(Context context, String key, Object defaultObject) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); if (defaultObject instanceof String) { return sp.getString(key, (String) defaultObject); } else if (defaultObject instanceof Integer) { return sp.getInt(key, (Integer) defaultObject); } else if (defaultObject instanceof Boolean) { return sp.getBoolean(key, (Boolean) defaultObject); } else if (defaultObject instanceof Float) { return sp.getFloat(key, (Float) defaultObject); } else if (defaultObject instanceof Long) { return sp.getLong(key, (Long) defaultObject); } return null; } /** * 存放数组 * * @param context * @param list * @param key */ public static void putList(Context context, List list, String key) { JSONArray jsonArray = new JSONArray(); for (int i = 0; i < list.size(); i++) { JSONObject object = new JSONObject(); try { object.put(i + "", list.get(i)); jsonArray.put(object); } catch (JSONException e) { e.printStackTrace(); } } put(context, key, jsonArray.toString()); } /** * 获取数组 * * @param context * @param key * @return list */ public static List getList(Context context, String key) { List list = new ArrayList(); try { JSONArray jsonArray = new JSONArray((String) get(context, key, "")); for (int i = 0; i < jsonArray.length(); i++) { JSONObject object = jsonArray.getJSONObject(i); list.add(i, object.getString(i + "")); } } catch (JSONException e) { e.printStackTrace(); } return list; } /** * 存入哈希表 * * @param context * @param map * @param key */ public static void putMap(Context context, Map<?, ?> map, String key) { JSONArray jsonArray = new JSONArray(); Set set = map.entrySet(); Iterator iterator = set.iterator(); for (int i = 0; i < map.size(); i++) { Map.Entry mapEntry = (Map.Entry) iterator.next(); try { JSONObject object = new JSONObject(); object.put("key", mapEntry.getKey()); object.put("value", mapEntry.getValue()); jsonArray.put(object); } catch (JSONException e) { e.printStackTrace(); } } put(context, key, jsonArray.toString()); } /** * 读取哈希表 * * @param context * @param key * @return map */ public static Map getMap(Context context, String key) { Map map = new HashMap(); try { JSONArray jsonArray = new JSONArray((String) get(context, key, "")); for (int i = 0; i < jsonArray.length(); i++) { try { JSONObject object = jsonArray.getJSONObject(i); map.put(object.getString("key"), object.getString("value")); } catch (JSONException e) { e.printStackTrace(); } } } catch (JSONException e) { e.printStackTrace(); } return map; } /** * 移除某个key值已经对应的值 * * @param context * @param key */ public static void remove(Context context, String key) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.remove(key); SharedPreferencesCompat.apply(editor); } /** * 清除所有数据 * * @param context */ public static void clear(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.clear(); SharedPreferencesCompat.apply(editor); } /** * 查询某个key是否存在 * * @param context * @param key * @return */ public static boolean contains(Context context, String key) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); return sp.contains(key); } /** * 返回所有的键值对 * * @param context * @return map */ public static Map<String, ?> getAll(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); return sp.getAll(); } /** * 解决SharedPreferencesCompat.apply方法的兼容类 * * @author liufengkai */ private static class SharedPreferencesCompat { private static final Method sApplyMethod = findApplyMethod(); /** * 反射查找apply的方法 * * @return method */ @SuppressWarnings({"unchecked", "rawtypes"}) private static Method findApplyMethod() { try { Class clz = SharedPreferences.Editor.class; return clz.getMethod("apply"); } catch (NoSuchMethodException e) { e.printStackTrace(); } return null; } /** * 如果找到则使用apply执行,否则使用commit * * @param editor */ public static void apply(SharedPreferences.Editor editor) { try { if (sApplyMethod != null) { sApplyMethod.invoke(editor); return; } } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } editor.commit(); } } }