4、Android之SharedPreferences使用及封装

时间:2021-12-26 16:42:10

1、定义

先来看看Android官方API对于SharedPreferences(以下简称SP)的定义:SP是通过getSharedPreferences(String,int)返回的用于访问和储存设置(Preferences,我本人理解为设置)数据的接口。对于许多特定的设置的数据集,存在一个单例的类能够将数据集给所有客户分享。修改设置数据必须通过SharedPreferences.Editor对象来确保设置的值保持一个一致的状态并且当它们提交时能受控制。SP对象通过get()方法必须在Application中保持一成不变。简单来说,SharedPreferences是一种轻型的数据存储方式,基于XML文件存储key-value键值对(只能存储简单的类型)数据,常用来存储一些简单的配置信息。

2、基本使用

下面是使用SharedPreferences的基本步骤:

  1. 获得SharedPreferences对象;
  2. 获得SharedPreferences.Editor对象;
  3. 通过Editor接口的putXxx方法保存key-value对其中Xxx表示不同的数据类型;
  4. 通过Editor接口的commit方法保存key-value对。

(注意:上述API定义已经说得很清楚了,SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象来实现的,所以SP对象不能直接用于写入数据)

核心代码如下:

SharedPreferences pref=PreferenceManager.                        getDefaultSharedPreferences(Context context);
//获取系统默认的SharedPreferences对象(Context中传入上下文,如MainActivity.this);
//或者:SharedPreferences pref = getSharedPreferences("XML文件名",“读取权限如MODE_PRIVATE");
Editor editor = pref.edit();//获得Editor对象;
editor.putString(key,value);//保存key-value对(有多种类型如putInt等);
editor.remove(key);//删除键值对;
editor.commit();//一定要提交;
pref.getString(key,defValue);//用于取值,key对应要取出的值的key,defValue是没取到值时的默认值;

3、SPUtil工具类封装

import android.content.Context;
import android.content.SharedPreferences;

import java.io.Serializable;
import java.util.Set;


public class SPStore {
//自己定义sp名称
public static String STORE_NAME_SETTING = "Wd_Study";

/**
* 获取sp句柄
* @param context 页面句柄
* @param storeName 存储名称
* @return
*/

private static SharedPreferences getSettingPreferences(Context context, String storeName) {
SharedPreferences setting = null;
setting = context.getSharedPreferences(storeName, Context.MODE_PRIVATE);
return setting;
}

/*------------------- string -----------------------*/

/**
* 获取指定sp名称中相应key值 <br>
* 使用样例:<br>
* SPStore.getString(ctx, "storeName", "key1")
* @param ctx 页面句柄
* @param storeName 保存名称
* @param key key值
* @return
*/

public static String getString(Context ctx, String storeName, String key) {
String value = null;
getSettingPreferences(ctx, storeName).edit();
if (getSettingPreferences(ctx, storeName).contains(key)) {
value = getSettingPreferences(ctx, storeName).getString(key, null);
}
return value;
}

/**
* 获取指定sp名称中相应key值 <br>
* 使用样例:<br>
* SPStore.getString(ctx, "storeName", "key1", "value1")
* @param ctx
* @param storeName
* @param key
* @param defaultValue 默认值
* @return
*/

public static String getString(Context ctx, String storeName, String key, String defaultValue) {
String value = null;
getSettingPreferences(ctx, storeName).edit();
if (getSettingPreferences(ctx, storeName).contains(key)) {
value = getSettingPreferences(ctx, storeName).getString(key, defaultValue);
}else {
value = defaultValue;
}
return value;
}

/**
* 获取sp中key对应的值
* 使用样例:<br>
* SPStore.getString(ctx, "storeName")
* @param ctx 页面句柄
* @param key key值
* @return
*/

public static String getString(Context ctx, String key) {
return getString(ctx, STORE_NAME_SETTING, key);
}

public static String getStringWithDefault(Context ctx, String key, String defaultValue) {
return getString(ctx, STORE_NAME_SETTING, key, defaultValue);
}

/**
* 存放数据到指定sp中
* @param ctx
* @param storeName
* @param key
* @param value
*/

public static void putString(Context ctx, String storeName, String key, Serializable value) {
SharedPreferences.Editor edit = getSettingPreferences(ctx, storeName).edit();
edit.putString(key, "" + value);
edit.commit();
}

/**
* 存放数据到sp中
* @param ctx
* @param key
* @param value
*/

public static void putString(Context ctx, String key, Serializable value) {
putString(ctx, STORE_NAME_SETTING, key, value);
}

/*------------------- int -----------------------*/

public static int getInt(Context ctx, String storeName, String key) {
int value = -1;
getSettingPreferences(ctx, storeName).edit();
if (getSettingPreferences(ctx, storeName).contains(key)) {
value = getSettingPreferences(ctx, storeName).getInt(key, -1);
}
return value;
}

public static int getInt(Context ctx, String storeName, String key, int defaultValue) {
int value = -1;
getSettingPreferences(ctx, storeName).edit();
if (getSettingPreferences(ctx, storeName).contains(key)) {
value = getSettingPreferences(ctx, storeName).getInt(key, defaultValue);
}else {
value = defaultValue;
}
return value;
}

public static int getInt(Context ctx, String key) {
return getInt(ctx, STORE_NAME_SETTING, key);
}

public static int getInt(Context ctx, String key, int defaultValue) {
return getInt(ctx, STORE_NAME_SETTING, key, defaultValue);
}

public static void putInt(Context ctx, String storeName, String key, int value) {
SharedPreferences.Editor edit = getSettingPreferences(ctx, storeName).edit();
edit.putInt(key, value);
edit.commit();
}

public static void putInt(Context ctx, String key, int value) {
putInt(ctx, STORE_NAME_SETTING, key, value);
}

/*------------------- long -----------------------*/

public static long getLong(Context ctx, String storeName, String key) {
long value = -1l;
getSettingPreferences(ctx, storeName).edit();
if (getSettingPreferences(ctx, storeName).contains(key)) {
value = getSettingPreferences(ctx, storeName).getLong(key, -1l);
}
return value;
}

public static long getLong(Context ctx, String storeName, String key, long defaultValue) {
long value = -1l;
getSettingPreferences(ctx, storeName).edit();
if (getSettingPreferences(ctx, storeName).contains(key)) {
value = getSettingPreferences(ctx, storeName).getLong(key, defaultValue);
}else {
value = defaultValue;
}
return value;
}

public static long getLong(Context ctx, String key) {
return getLong(ctx, STORE_NAME_SETTING, key);
}

public static long getLong(Context ctx, String key, long defaultValue) {
return getLong(ctx, STORE_NAME_SETTING, key, defaultValue);
}

public static void putLong(Context ctx, String storeName, String key, long value) {
SharedPreferences.Editor edit = getSettingPreferences(ctx, storeName).edit();
edit.putLong(key, value);
edit.commit();
}

public static void putLong(Context ctx, String key, long value) {
putLong(ctx, STORE_NAME_SETTING, key, value);
}

/*------------------- float -----------------------*/

public static float getFloat(Context ctx, String storeName, String key) {
float value = -1f;
getSettingPreferences(ctx, storeName).edit();
if (getSettingPreferences(ctx, storeName).contains(key)) {
value = getSettingPreferences(ctx, storeName).getFloat(key, -1f);
}
return value;
}

public static float getFloat(Context ctx, String storeName, String key, float defaultValue) {
float value = -1f;
getSettingPreferences(ctx, storeName).edit();
if (getSettingPreferences(ctx, storeName).contains(key)) {
value = getSettingPreferences(ctx, storeName).getFloat(key, defaultValue);
}else {
value = defaultValue;
}
return value;
}

public static float getFloat(Context ctx, String key) {
return getFloat(ctx, STORE_NAME_SETTING, key);
}

public static float getFloat(Context ctx, String key, float defaultValue) {
return getFloat(ctx, STORE_NAME_SETTING, key, defaultValue);
}

public static void putFloat(Context ctx, String storeName, String key, float value) {
SharedPreferences.Editor edit = getSettingPreferences(ctx, storeName).edit();
edit.putFloat(key, value);
edit.commit();
}

public static void putFloat(Context ctx, String key, float value) {
putFloat(ctx, STORE_NAME_SETTING, key, value);
}

/*------------------- bool -----------------------*/

public static boolean getBoolean(Context ctx, String storeName, String key) {
boolean value = false;
getSettingPreferences(ctx, storeName).edit();
if (getSettingPreferences(ctx, storeName).contains(key)) {
value = getSettingPreferences(ctx, storeName).getBoolean(key, false);
}
return value;
}

public static boolean getBoolean(Context ctx, String storeName, String key, boolean defaultValue) {
boolean value = false;
getSettingPreferences(ctx, storeName).edit();
if (getSettingPreferences(ctx, storeName).contains(key)) {
value = getSettingPreferences(ctx, storeName).getBoolean(key, defaultValue);
}else {
value = defaultValue;
}
return value;
}

public static boolean getBoolean(Context ctx, String key) {
return getBoolean(ctx, STORE_NAME_SETTING, key);
}

public static boolean getBoolean(Context ctx, String key, boolean defaultValue) {
return getBoolean(ctx, STORE_NAME_SETTING, key, defaultValue);
}

public static void putBoolean(Context ctx, String storeName, String key, boolean value) {
SharedPreferences.Editor edit = getSettingPreferences(ctx, storeName).edit();
edit.putBoolean(key, value);
edit.commit();
}

public static void putBoolean(Context ctx, String key, boolean value) {
putBoolean(ctx, STORE_NAME_SETTING, key, value);
}

/*------------------- set string -----------------------*/

public static Set<String> getStringSet(Context ctx, String storeName, String key) {
Set<String> value = null;
getSettingPreferences(ctx, storeName).edit();
if (getSettingPreferences(ctx, storeName).contains(key)) {
value = getSettingPreferences(ctx, storeName).getStringSet(key, null);
}
return value;
}

public static Set<String> getStringSet(Context ctx, String key) {
return getStringSet(ctx, STORE_NAME_SETTING, key);
}

public static void putStringSet(Context ctx, String storeName, String key, Set<String> value) {
SharedPreferences.Editor edit = getSettingPreferences(ctx, storeName).edit();
edit.putStringSet(key, value);
edit.commit();
}

public static void putStringSet(Context ctx, String key, Set<String> value) {
putStringSet(ctx, STORE_NAME_SETTING, key, value);
}

/*------------------- object -----------------------*/

// TODO: 16/7/6 未实现

// public static <T> void getObject(Context ctx, String storeName, String key, T resObj) {
// String value = null;
// getSettingPreferences(ctx, storeName).edit();
// if (getSettingPreferences(ctx, storeName).contains(key)) {
// value = getSettingPreferences(ctx, storeName).getString(key, null);
// }
// Gson gson = new GsonBuilder().create();
// resObj = gson.fromJson(value, T.class);
// }
//
// public static String getString(Context ctx, String key) {
// return getString(ctx, STORE_NAME_SETTING, key);
// }
//
// public static void putString(Context ctx, String storeName, String key, Serializable value) {
// SharedPreferences.Editor edit = getSettingPreferences(ctx, storeName).edit();
// edit.putString(key, "" + value);
// edit.commit();
// }
//
// public static void putString(Context ctx, String key, Serializable value) {
// putString(ctx, STORE_NAME_SETTING, key, value);
// }

public static Set<String> getAllKey(Context context) {
return getSettingPreferences(context, STORE_NAME_SETTING).getAll().keySet();
}

public static Set<String> getAllKey(Context context, String storeName) {
return getSettingPreferences(context, storeName).getAll().keySet();
}

public static void clearStoreName(Context context, String storeName) {
SharedPreferences.Editor edit = getSettingPreferences(context, storeName).edit();
edit.clear();
edit.apply();
}
}