/*************************************************************************** * 封装sharedPreferences SettingsSPData * 说明: * 主要为了解决每次使用sharedPreferences的时候,都要操作很多次同样的操作, * 于是将很多操作直接放到一起来, 图个省事,以后不用每次都去操作这些相同的操作。 * * 2016-5-29 深圳 南山平山村 曾剑锋 **************************************************************************/ package com.android.gpiofuncctrl; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.util.Log; public class SettingsSPData{ /** * The name for default sharedPreferences file name. */ private final static String SETTING_SP = "SettingsSPData"; /** * The mode for default sharePreferences file. */ private final static int SP_MODE = Context.MODE_PRIVATE; /** * The activity for convenient get sharePreferences at anywhere. */ private static Activity activity = null; /** * The tag for debug. */ private final static String TAG = "SettingsSPData"; public static void setActivity(Activity activity) { if (activity == null) { Log.d(TAG, "Setting Activity was null, please check this."); return ; } SettingsSPData.activity = activity; } public static Activity getActivity() { return SettingsSPData.activity; } public static SharedPreferences getSP(Activity activity, String name) { return activity.getSharedPreferences(name, SP_MODE); } public static SharedPreferences getSP(Activity activity) { return getSP(activity, SETTING_SP); } private static boolean checkObject(Object object) { return (object != null); } private static boolean checkString(String key) { return (checkObject(key) && key.trim().length() != 0); } private static boolean checkActivityAndString(Activity activity, String name){ return (checkObject(activity) && checkString(name)); } private static boolean checkActivityAndStrings(Activity activity, String name, String key) { return (checkActivityAndOject(activity, name) && checkString(key)); } private static boolean checkActivityAndStrings(Activity activity, String name, String key, String value){ return (checkActivityAndStrings(activity, name, key) && checkString(value)); } private static boolean checkActivityAndOject(Activity activity, Object object){ return (checkObject(activity) && checkObject(object)); } public static String getString(Activity activity, String name, String key) { if (checkActivityAndStrings(activity, name, key)) { return getSP(activity, name).getString(key, "").trim(); } return ""; } public static String getString(Activity activity, String key) { return getString(activity, SETTING_SP, key); } public static String getString(String key) { return getString(activity, SETTING_SP, key); } public static void pushString(Activity activity, String name, String key, String value) { if (checkActivityAndStrings(activity, name, key, value)) { getSP(activity, name).edit().putString(key, value.trim()).commit(); } } public static void pushString(Activity activity, String key, String value) { pushString(activity, SETTING_SP, key, value); } public static void pushString(String key, String value) { pushString(activity, SETTING_SP, key, value); } public static int getInt(Activity activity, String name, String key) { if (checkActivityAndStrings(activity, name, key)) { return getSP(activity, name).getInt(key, 0); } return 0; } public static int getInt(Activity activity, String key) { return getInt(activity, SETTING_SP, key); } public static int getInt(String key) { return getInt(activity, SETTING_SP, key); } public static void pushInt(Activity activity, String name, String key, int value) { if (checkActivityAndStrings(activity, name, key)) { getSP(activity, name).edit().putInt(key, value).commit(); } } public static void pushInt(Activity activity, String key, int value) { pushInt(activity, SETTING_SP, key, value); } public static void pushInt(String key, int value) { pushInt(activity, SETTING_SP, key, value); } public static float getFloat(Activity activity, String name, String key) { if (checkActivityAndStrings(activity, name, key)) { return getSP(activity, name).getFloat(key, 0); } return 0; } public static float getFloat(Activity activity, String key) { return getFloat(activity, SETTING_SP, key); } public static float getFloat(String key) { return getFloat(activity, SETTING_SP, key); } public static void pushFloat(Activity activity, String name, String key, float value) { if (checkActivityAndStrings(activity, name, key)) { getSP(activity, name).edit().putFloat(key, value).commit(); } } public static void pushFloat(Activity activity, String key, float value) { pushFloat(activity, SETTING_SP, key, value); } public static void pushFloat(String key, float value) { pushFloat(activity, SETTING_SP, key, value); } public static long getLong(Activity activity, String name, String key) { if (checkActivityAndStrings(activity, name, key)) { return getSP(activity, name).getLong(key, 0); } return 0; } public static long getLong(Activity activity, String key) { return getLong(activity, SETTING_SP, key); } public static long getLong(String key) { return getLong(activity, SETTING_SP, key); } public static void pushLong(Activity activity, String name, String key, long value) { if (checkActivityAndStrings(activity, name, key)) { getSP(activity, name).edit().putLong(key, value).commit(); } } public static void pushLong(Activity activity, String key, long value) { pushLong(activity, SETTING_SP, key, value); } public static void pushLong(String key, long value) { pushLong(activity, SETTING_SP, key, value); } public static boolean getBoolean(Activity activity, String name, String key) { if (checkActivityAndStrings(activity, name, key)) { return getSP(activity, name).getBoolean(key, false); } return false; } public static boolean getBoolean(Activity activity, String key) { return getBoolean(activity, SETTING_SP, key); } public static boolean getBoolean(String key) { return getBoolean(activity, SETTING_SP, key); } public static void pushBoolean(Activity activity, String name, String key, boolean value) { if (checkActivityAndStrings(activity, name, key)) { getSP(activity, name).edit().putBoolean(key, value).commit(); } } public static void pushBoolean(Activity activity, String key, boolean value) { pushBoolean(activity, SETTING_SP, key, value); } public static void pushBoolean(String key, boolean value) { pushBoolean(activity, SETTING_SP, key, value); } private static void pushToSP(Editor editor, String key, Object value) { if (value instanceof String) { editor.putString(key, ((String)value).trim()); } else if (value instanceof Integer) { editor.putInt(key, (Integer)value); } else if (value instanceof Float) { editor.putFloat(key, (Float)value); } else if (value instanceof Long) { editor.putLong(key, (Long)value); } else if (value instanceof Boolean) { editor.putBoolean(key, (Boolean)value); } } public static void pushValue(Activity activity, String name, String key, Object value) { if (checkActivityAndStrings(activity, name, key)) { Editor editor = getSP(activity, name).edit(); pushToSP(editor, key, value); editor.commit(); } } public static void pushValue(Activity activity, String key, Object value) { pushValue(activity, SETTING_SP, key, value); } public static void pushValue(String key, Object value) { pushValue(activity, SETTING_SP, key, value); } public static void pushValues(Activity activity, String name, HashMap<String, Object> keyValues) { if (checkActivityAndOject(activity, keyValues)) { Editor editor = getSP(activity, name).edit(); Iterator<Entry<String, Object>> iterator = keyValues.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iterator.next(); String key = (String)entry.getKey(); Object value = entry.getValue(); pushToSP(editor, key, value); } editor.commit(); } } public static void pushValues(Activity activity, HashMap<String, Object> keyValues) { pushValues(activity, SETTING_SP, keyValues); } public static void putValues(HashMap<String, Object> keyValues) { pushValues(activity, SETTING_SP, keyValues); } private static Object getFromSP(SharedPreferences sp, String key, Object type) { Object value = null; if (type instanceof String) { value = sp.getString(key, "").trim(); } else if (type instanceof Integer) { value = sp.getInt(key, 0); } else if (type instanceof Float) { value = sp.getFloat(key, 0); } else if (type instanceof Long) { value = sp.getLong(key, 0); } else if (type instanceof Boolean) { value = sp.getBoolean(key, false); } return value; } public static Object getValue(Activity activity, String name, String key, Object type) { if (checkActivityAndStrings(activity, name, key)) { SharedPreferences sp = getSP(activity, name); return getFromSP(sp, key, type); } return null; } public static Object getValue(Activity activity, String key, Object type) { return getValue(activity, SETTING_SP, key, type); } public static Object getValue(String key, Object type) { return getValue(activity, SETTING_SP, key, type); } public static HashMap<String, Object> getValues(Activity activity, String name, HashMap<String, Object> keyTypes) { HashMap<String, Object> keyValues = new HashMap<String, Object>(); if (checkActivityAndString(activity, name)) { SharedPreferences sp = getSP(activity, name); Iterator<Entry<String, Object>> iter = keyTypes.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iter.next(); String key = (String)entry.getKey(); Object type = entry.getValue(); keyValues.put(key, getFromSP(sp, key, type)); } return keyValues; } return keyValues; } public static HashMap<String, Object> getValues(Activity activity, HashMap<String, Object> keyTypes) { return getValues(activity, SETTING_SP, keyTypes); } public static HashMap<String, Object> getValues(HashMap<String, Object> keyTypes) { return getValues(activity, SETTING_SP, keyTypes); } public static void testSettingsSPData(Activity activity) { setActivity(activity); pushString("name", "zengjf"); Log.e("SettingsSPData", "name = " + (String)getValue("name", new String())); pushInt("int", 3); Log.e("SettingsSPData", "int = " + getValue("int", Integer.valueOf(0))); pushFloat("float", 3.14f); Log.e("SettingsSPData", "float = " + getValue("float", Float.valueOf(0))); HashMap<String, Object> keyvalues = new HashMap<String, Object>(); keyvalues.put("name", "zengsf"); keyvalues.put("address", "shenzhen"); keyvalues.put("float", 3.14f); keyvalues.put("int", 3); keyvalues.put("boolean", true); pushValues(activity, keyvalues); HashMap<String, Object> keyTypes = new HashMap<String, Object>(); keyTypes.put("name", new String()); keyTypes.put("address", new String()); keyTypes.put("float", Float.valueOf(0)); keyTypes.put("int", Integer.valueOf(0)); keyTypes.put("boolean", Boolean.valueOf(false)); Log.e(TAG, getValues(keyTypes).toString()); Log.e("SettingsSPData", "age = " + getValue("age", Integer.valueOf(0))); Log.e("SettingsSPData", "int = " + getValue("int", Integer.valueOf(0))); Log.e("SettingsSPData", "float = " + getValue("float", Float.valueOf(0))); Log.e("SettingsSPData", "name = " + (String)getValue("name", new String())); /** * output: * E/SettingsSPData(8456): name = zengjf * E/SettingsSPData(8456): int = 3 * E/SettingsSPData(8456): float = 3.14 * E/SettingsSPData(8456): {boolean=true, address=shenzhen, float=3.14, int=3, name=zengsf} * E/SettingsSPData(8456): age = 0 * E/SettingsSPData(8456): int = 3 * E/SettingsSPData(8456): float = 3.14 * E/SettingsSPData(8633): name = zengsf */ } }