博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
封装sharedPreferences SettingsSPData
阅读量:6694 次
发布时间:2019-06-25

本文共 12594 字,大约阅读时间需要 41 分钟。

/*************************************************************************** *                封装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
keyValues) { if (checkActivityAndOject(activity, keyValues)) { Editor editor = getSP(activity, name).edit(); Iterator
> iterator = keyValues.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry
entry = (Map.Entry
) iterator.next(); String key = (String)entry.getKey(); Object value = entry.getValue(); pushToSP(editor, key, value); } editor.commit(); } } public static void pushValues(Activity activity, HashMap
keyValues) { pushValues(activity, SETTING_SP, keyValues); } public static void putValues(HashMap
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
getValues(Activity activity, String name, HashMap
keyTypes) { HashMap
keyValues = new HashMap
(); if (checkActivityAndString(activity, name)) { SharedPreferences sp = getSP(activity, name); Iterator
> iter = keyTypes.entrySet().iterator(); while (iter.hasNext()) { Map.Entry
entry = (Map.Entry
) 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
getValues(Activity activity, HashMap
keyTypes) { return getValues(activity, SETTING_SP, keyTypes); } public static HashMap
getValues(HashMap
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
keyvalues = new HashMap
(); 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
keyTypes = new HashMap
(); 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 */ }}

 

转载地址:http://otjoo.baihongyu.com/

你可能感兴趣的文章
办公室趣事
查看>>
Ubuntu Server添加磁盘
查看>>
复合索引
查看>>
ASA SSL××× 配置
查看>>
51CTO博客将于2017年7月完成首页改版(7月11日下午上线啦)
查看>>
微信公众帐号开发教程第1篇-引言
查看>>
初学Linux--shell
查看>>
LNMP环境部署
查看>>
SQL SERVER 对检索结果排序
查看>>
CCFlow建设工程流程管理系统应用案例
查看>>
服务器配置之DNS服务器
查看>>
PHP适配器模式
查看>>
性能测试工具sysbench简介
查看>>
网络综合测试--5
查看>>
Centos 6.5 离线安装包下载地址
查看>>
字符串相似度
查看>>
CMAKE方式编译安装Mysql5.5
查看>>
日志服务器管理网络设备日志(Cisco;华为;f5;netscreen;)
查看>>
统计单词个数
查看>>
springcloud中配置服务actuator的小坑
查看>>