一.SharePreferences概述。
SharePreferences是用来存储一些简单配置信息的一种机制,使用Map数据结构来存储数据,以键值对的方式存储,采用了XML格式将数据存储到设备中。例如保存登录用户的用户名和密码。只能在同一个包内使用,不能在不同的包之间使用,其实也就是说只能在创建它的应用中使用,其他应用无法使用。
创建的存储文件保存在/data/data//shares_prefs文件夹下。
二.SharePreferences的使用。
通过Context.getSharedPreferences方法获取SharedPreferences对象,参数分别为存储的文件名和存储模式。
// 获取SharedPreferences对象
SharedPreferences sp = getSharedPreferences(DATABASE, Activity.MODE_PRIVATE);
// 获取Editor对象
Editor editor = sp.edit();
三…sharedpreferences的具体用法。
SharePreferences存储数据是通过获取Editor编辑器对象来操作的。
插入数据:
调用Editor.putxxxx方法,两个参数分别为键和值。
SharedPreferences的代码片段
// 存储sharedpreferences
public void setSharedPreference() {
sharedPreferences = getSharedPreferences(“itcast”, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString(“username”, text1.getText().toString());
editor.putInt(“password”, getpw());
editor.commit();// 提交修改
}
获取数据:
调用Editor.getxxxx方法,两个参数分别为键和不存在指定键时的默认值。
SharedPreferences的代码片段
// 获得sharedpreferences的数据
public void getSahrePreference() {
String username = sharedPreferences.getString(“username”, “”);
int password = sharedPreferences.getInt(“password”, 0);
String str = String.valueOf(password);
text1.setText(username);
text2.setText(str);
删除数据:
调用Editor.remove方法,参数为指定的键。
SharedPreferences的代码片段
// 清除sharedpreferences的数据
public void removeSharedPreference() {
sharedPreferences = getSharedPreferences(“itcast”, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.remove(“username”);
editor.remove(“password”);
editor.commit();// 提交修改
}
清空所有数据:
调用Editor.clear方法
上述所有方法调用都要执行Editor.commit方法来提交。
1、使用SharedPreferences保存数据方法如下:
//实例化SharedPreferences对象(第一步)
SharedPreferences mySharedPreferences= getSharedPreferences(“test”,
Activity.MODE_PRIVATE);
//实例化SharedPreferences.Editor对象(第二步)
SharedPreferences.Editor editor = mySharedPreferences.edit();
//用putString的方法保存数据
editor.putString(“name”, “Karl”);
editor.putString(“habit”, “sleep”);
//提交当前数据
editor.commit();
//使用toast信息提示框提示成功写入数据
Toast.makeText(this, “数据成功写入SharedPreferences!” , Toast.LENGTH_LONG).show();
2、使用SharedPreferences读取数据方法如下:
//同样,在读取SharedPreferences数据前要实例化出一个SharedPreferences对象
SharedPreferencessharedPreferences= getSharedPreferences(“test”,
Activity.MODE_PRIVATE);
// 使用getString方法获得value,注意第2个参数是value的默认值
String name =sharedPreferences.getString(“name”, “”);
String habit =sharedPreferences.getString(“habit”, “”);
//使用toast信息提示框显示信息
Toast.makeText(this, “读取数据如下:”+"\n"+“name:” + name + “\n” + “habit:” + habit,
Toast.LENGTH_LONG).show();
3、SharedPreferences的四种操作模式:
Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
四.下面提供一个SharedPreferences工具类,在开发中直接调用即可。
- SharedPreferences存储数据方式工具类
- @author zuolongsnail
*/
public class SharedPrefsUtil {
public final static String SETTING = “Setting”;
public static void putValue(Context context,String key, int value) {
Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putInt(key, value);
sp.commit();
}
public static void putValue(Context context,String key, boolean value) {
Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putBoolean(key, value);
sp.commit();
}
public static void putValue(Context context,String key, String value) {
Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putString(key, value);
sp.commit();
}
public static int getValue(Context context,String key, int defValue) {
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
int value = sp.getInt(key, defValue);
return value;
}
public static boolean getValue(Context context,String key, boolean defValue) {
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
boolean value = sp.getBoolean(key, defValue);
return value;
}
public static String getValue(Context context,String key, String defValue) {
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
String value = sp.getString(key, defValue);
return value;
}
}