Android数据持久化:
是指将那些内存中的瞬时数据保存到存储设备中,保证即使手机关机的情况下,数据仍然不会丢失;
保存在内存中的数据是处于瞬时状态的,而保存在设备中的数据是处于持久状态的;
持久化技术提供了一种机制可以让数据在瞬时状态和持久状态之间进行转换。
一、Android中偏好存储
1. 是什么?
是Android中常见的一种数据存储方式,主要通过键值对的方式存储数据;
只能存储少量数据格式简单的数据,例如boolean,int,float,long,String,StringSet六种简单的数据类型。
2.存储位置
存储位置在 /data/data/app_package/shared_prefs/ 目录下;
3.使用步骤
存储:
- 根据Contex获取SharedPreferences对象
- 调用SharedPreferences对象的edit方法来获取SharedPreferences.Editor对象
- 向SharedPreferences.Editor对象中添加数据
- 调用apply方法将添加的数据提交,从而完成数据存储操作
代码演示:
/**
* 声明文件名
*/
private String fileName = "appConfig";
/**
* 存储
*/
private void savePreferData() {
//根据Context获取SharedPreferences对象,activity中可不加context,
//其他地方要使用context.getSharedPreferences
SharedPreferences sp = getSharedPreferences(fileName, MODE_PRIVATE);
//SharedPreferences.Editor对象
SharedPreferences.Editor edit = sp.edit();
//向SharedPreferences.Editor对象中添加数据
edit.putInt("age", 1);
edit.putString("name", "test");
edit.putFloat("height", 160f);
edit.putBoolean("merry", true);
//将添加的数据提交
edit.apply();
}
获取:
- 根据Contex获取SharedPreferences对象
- 使用调用SharedPreferences对象获取不同类型的键对应的值;
代码演示:
/**
* 声明文件名
*/
private String fileName = "appConfig";
/**
* 获取
*/
private void getPreferData() {
//根据Context获取SharedPreferences对象,activity中可不加context,
//其他地方要使用context.getSharedPreferences
SharedPreferences sp = getSharedPreferences(fileName, MODE_PRIVATE);
//根据之前存储的键值对,获取对应键的值
int age = sp.getInt("age", 0);
String name = sp.getString("name", "");
float height = sp.getFloat("height", 0f);
boolean merry = sp.getBoolean("merry", false);
Log.e("TAG", "getPreferData: "
+ "age: " + age
+ "name: " + name
+ "height :" + height
+ "merry: " + merry);
}
4.偏好存储工具类的封装
封装基础类
public class SharePreferenceUtil {
private final static String SP_NAME = "config";
/**
* set方法
*/
public static void setInt(Context context, String key, int value) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
sp.edit().putInt(key, value).apply();
}
}
public static void setString(Context context, String key, String value) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
sp.edit().putString(key, value).apply();
}
}
public static void setFloat(Context context, String key, Float value) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
sp.edit().putFloat(key, value).apply();
}
}
public static void setLong(Context context, String key, long value) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
sp.edit().putLong(key, value).apply();
}
}
public static void setBoolean(Context context, String key, boolean value) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
sp.edit().putBoolean(key, value).apply();
}
}
/**
* get方法
*/
public static int getInt(Context context, String key, int defaultValue) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
return sp.getInt(key, defaultValue);
}
return defaultValue;
}
public static String getString(Context context, String key, String defaultValue) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
return sp.getString(key, defaultValue);
}
return defaultValue;
}
public static float getFloat(Context context, String key, float defaultValue) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
return sp.getFloat(key, defaultValue);
}
return defaultValue;
}
public static long getLong(Context context, String key, long defaultValue) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
return sp.getLong(key, defaultValue);
}
return defaultValue;
}
public static boolean getBoolean(Context context, String key, boolean defaultValue) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
return sp.getBoolean(key, defaultValue);
}
return defaultValue;
}
/**
* remove方法
*/
public static void removeString(Context context, String key) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
sp.edit().remove(key).apply();
}
}
public static void remove(Context context, String key) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
sp.edit().remove(key).apply();
}
}
public static void clearAll(Context context) {
if (null != context) {
SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
sp.edit().clear().apply();
}
}
}
封装中转类
public class SharePreferencefHelper {
//int
public static void setAppVersionCode(Context context, int splitMode) {
SharePreferenceUtil.setInt(context, "version_code", splitMode);
}
public static int getAppVersionCode(Context context, int defValue) {
return SharePreferenceUtil.getInt(context, "version_code", defValue);
}
//String
public static void setUserName(Context context, String value) {
SharePreferenceUtil.setString(context, "user_name", value);
}
public static String getUserName(Context context) {
return SharePreferenceUtil.getString(context, "user_name", "");
}
//long
public static void setInstallData(Context context, long milliSeconds) {
SharePreferenceUtil.setLong(context, "install_data", milliSeconds);
}
public static long getInstallData(Context context) {
return SharePreferenceUtil.getLong(context, "install_data", 0);
}
//boolean
public static void setFirstInstallState(Context context, boolean defValue) {
SharePreferenceUtil.setBoolean(context, "first_install", defValue);
}
public static boolean getFirstInstallState(Context context, boolean defValue) {
return SharePreferenceUtil.getBoolean(context, "first_install", defValue);
}
}
调用
//此处的this指的是当前上下文,即getApplicationContext 或 当前Activity
SharePreferencefHelper.setUserName(this, "admin");
String userName = SharePreferencefHelper.getUserName(this);
仍然存在优化:
可以进一步对 key进行封装,以便进行统一管理;
如果小伙伴发现书写中出现错误,或者有更好的封装办法,欢迎留言和私信。