一、获取SharedPreferences实例的方式
1、Activity方式:public SharedPreferences getPreferences(int mode):用这种方式获取的SharedPreferences对象,读写
都必须在同一个Activity中进行。
2、ContextWrapper方式:public SharedPreferences getSharedPreferences(String name,int mode):用这种方式获取的是
用户自定义的xml文件,可以在应用程序中任何地方进行,只要有ContextWrapper对象就行。
3、PreferenceManager方式:public static SharedPreferences getDefaultSharedPreferences(Context context):用这种方
式获取的是包名_preferences.xml文件,在应用程序中任何地方进行。
二、特殊用法
在我们使用SharedPreference的时候,常用的就是存储配置文件信息,但有时我们需要存储多维数组信息的时候,就可以用到putStringSet。
下面是写方法:
[html]
public void SaveSiteinfoToXml() {
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
Editor editor = prefs.edit();
Set<String> siteno = new HashSet<String>();
if (Unit_PublicVar.arr_DeatilContent != null) {
for (int i = 0; i < Unit_PublicVar.arr_DeatilContent.length; i++) {
siteno.add(Unit_PublicVar.arr_DeatilContent[i][0] + ","
+ Unit_PublicVar.arr_DeatilContent[i][1] + ","
+ Unit_PublicVar.arr_DeatilContent[i][2] + ","
+ Unit_PublicVar.arr_DeatilContent[i][3]); //
}
}
editor.putStringSet("站点信息", siteno);
editor.commit();
}
putStringset需要传入Set<String> 类型的参数 ,本函数的二维数组之前已经有值了,所以此处直接存储是没有问题的。
下面是读方法
[java]
public void LoadParaFromXml() {
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
Set<String> siteno = new HashSet<String>();
siteno = prefs.getStringSet("站点信息", siteno);
if (siteno.size() > 0) {
String[] data = (String[]) siteno.toArray(new String[siteno.size()]); //将SET转换为数组
Unit_PublicVar.arr_DeatilContent = new String[data.length][];
for (int i = 0; i < data.length; i++) {
Unit_PublicVar.arr_DeatilContent[i] = data[i].trim().split(
",");
}
}
}
三、特殊用法
Android3.0以上版本中 SharedPreferences新增了函数
- abstract Set<String> getStringSet(String key, Set<String> defValues)
- Retrieve a set of String values from the preferences.
abstract Set<String> getStringSet(String key, Set<String> defValues)
Retrieve a set of String values from the preferences.
同时SharedPreferences.Editor中也新增了函数
- abstract SharedPreferences.Editor putStringSet(String key, Set<String> values)
- Set a set of String values in the preferences editor, to be written back once commit() is called.
abstract SharedPreferences.Editor putStringSet(String key, Set<String> values)
Set a set of String values in the preferences editor, to be written back once commit() is called.
这样可以直接存储一个字符串集合,但是android3.0以前的版本中没有,如何在android2.3的系统中寻找一种替代他们的方式,我们可以猜想下上面两个函数的实现,直接看代码:
- public class SharedPreferencesHandler {
- final static String regularEx = "|";
- public static Set<String> getStringSet(SharedPreferences prefs, String key,
- Set<String> defValues) {
- String str = prefs.getString(key, "");
- if (!str.isEmpty()) {
- String[] values = str.split(regularEx);
- if (defValues == null) {
- defValues = new HashSet<String>();
- for (String value : values) {
- if (!value.isEmpty()) {
- defValues.add(value);
- }
- }
- }
- }
- return defValues;
- }
- public static SharedPreferences.Editor putStringSet(
- SharedPreferences.Editor ed, String key, Set<String> values) {
- String str = "";
- if (values != null | !values.isEmpty()) {
- Object[] objects = values.toArray();
- for (Object obj : objects) {
- str += obj.toString();
- str += regularEx;
- }
- ed.putString(key, str);
- }
- return ed;
- }
- }
public class SharedPreferencesHandler {
final static String regularEx = "|";
public static Set<String> getStringSet(SharedPreferences prefs, String key,
Set<String> defValues) {
String str = prefs.getString(key, "");
if (!str.isEmpty()) {
String[] values = str.split(regularEx);
if (defValues == null) {
defValues = new HashSet<String>();
for (String value : values) {
if (!value.isEmpty()) {
defValues.add(value);
}
}
}
}
return defValues;
}
public static SharedPreferences.Editor putStringSet(
SharedPreferences.Editor ed, String key, Set<String> values) {
String str = "";
if (values != null | !values.isEmpty()) {
Object[] objects = values.toArray();
for (Object obj : objects) {
str += obj.toString();
str += regularEx;
}
ed.putString(key, str);
}
return ed;
}
}
然后使用的时候可以这样存储和获取一个字符串的集合:
- final Set<String> snoozedIds = SharedPreferencesHandler
- .getStringSet(prefs, PREF_SNOOZE_IDS, new HashSet<String>());
- // prefs.getStringSet(PREF_SNOOZE_IDS,
- // new HashSet<String>());
- snoozedIds.add(Integer.toString(id));
- final SharedPreferences.Editor ed = prefs.edit();
- SharedPreferencesHandler.putStringSet(ed, PREF_SNOOZE_IDS,
- snoozedIds);
- // ed.putStringSet(PREF_SNOOZE_IDS, snoozedIds);
- ed.putLong(getAirPrefSnoozeTimeKey(id), time);
- ed.apply();