应用
Android开发中,需要将少量简单类型的数据保存在本地,比如几个字符串,一般选择使用SharedPreferences来保存。
SharedPreferences:一个轻量级的存储类,特别适合用于保存软件配置参数。使用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs。可以保存的数据类型有:int、boolean、float、long、String、StringSet。
存储数据
- 使用Activity类的getSharedPreferences方法获得SharedPreferences对象;
- 使用SharedPreferences接口的edit获得SharedPreferences.Editor对象;
- 通过SharedPreferences.Editor接口的putXXX方法保存key-value对;
- 通过过SharedPreferences.Editor接口的commit(或apply)方法保存key-value对。
读取数据
- 使用Activity类的getSharedPreferences方法获得SharedPreferences对象;
- 通过SharedPreferences对象的getXXX方法获取数据。
获取SharedPreferences对象:
public SharedPreferences getSharedPreferences (String name, int mode);
name: 自定义命名
mode: 模式
MODE_PRIVATE 只能被自己的应用程序访问
MODE_WORLD_READABLE 可以被自己的应用程序和其它应用程序读取
MODE_WORLD_WRITEABLE 可以被自己的应用程序和其它应用程序读取和写入
当Activity只需要创建一个SharedPreferences对象时,可以使用getPreferences方法
public SharedPreferences getPreferences (int mode);
获取Editor对象:
SharedPreferences.Editor edit = mSharedPre.edit( );
写入数据:
edit. putXXX ( key , value);
移除指定key的数据:
edit.remove(key);
清空数据:
edit.clear();
提交数据:
edit.commit();//或Edit.apply
edit.apply 和 edit.commit区别:
- apply没有返回值,commit返回boolean 表明是否提交成功。
- apply将修改数据原子提交到内存,然后异步提交到硬件磁盘。Commit 是同步提交。前者效率更高。
提交数据:
edit. getXXX ( key , defValue);
SharedPreferences 存取 List
以下封装了SharedPreferences 存储、读取、删除List集合数据的方法。
public class SharedPreferenceUtil {
public static final String SHARED_KEY = "shared_key";
private static int MAX_LIST_SIZE = 10;
private Context mContext;
private Activity mActivity;
SharedPreferences mSharedPreferences;
public SharedPreferenceUtil(Context mContext, Activity mActivity) {
this.mContext = mContext;
this.mActivity = mActivity;
getCurSharedPreferences();
}
private SharedPreferences getCurSharedPreferences() {
mSharedPreferences = mActivity.getPreferences(mContext.MODE_PRIVATE);
return mSharedPreferences;
}
//移除一条数据
public void removeItem(String data) {
//首先获取已经保存的list
ArrayList<String> curList = getStringArrayPreference(SHARED_KEY);
//从集合中移出data
curList.remove(data);
//重新保存list
setStringArrayPreference(SHARED_KEY, curList);
}
//新保存一条数据
public void addItem(String data) {
int newMaxListSize;
ArrayList<String> newList = new ArrayList<>();
newList.add(data);
ArrayList<String> curList = getStringArrayPreference(SHARED_KEY);
if (curList.contains(data)) {
newMaxListSize = MAX_LIST_SIZE;
} else {
newMaxListSize = MAX_LIST_SIZE - 1;
}
for (int i = 0; i < Math.min(curList.size(), newMaxListSize); i++) {
if (!data.equals(curList.get(i))) {
newList.add(curList.get(i));
}
}
setStringArrayPreference(SHARED_KEY, newList);
}
//清空所有保存的数据
public void clearAllData() {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.clear();
editor.apply();
}
public void setStringArrayPreference(String key, ArrayList<String> mList) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
JSONArray mJSONArray = new JSONArray();
//将mList的内容存进mJSONArray中
for (int i = 0; i < mList.size(); i++) {
mJSONArray.put(mList.get(i));
}
//写入数据
if (!mList.isEmpty()) {
editor.putString(key, mJSONArray.toString());
} else {
editor.putString(key, null);
}
//提交数据
editor.apply();
}
public ArrayList<String> getStringArrayPreference(String key) {
//读取保存的数据
String json = mSharedPreferences.getString(key, null);
ArrayList<String> mList = new ArrayList<>();
if (json != null) {
try {
JSONArray mJSONArray = new JSONArray(json);
//读取保存的内容并存进ArrayList数组中
for (int i = 0; i < mJSONArray.length(); i++) {
String data = mJSONArray.optString(i);
mList.add(data);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return mList;
}
}
END.