建议多了解一下SharedPreferences的源码,这里附作者认为写的不错的大神的链接
https://www.jianshu.com/p/8eb2147c328b/
本文章不做过多的源码原理方面的探讨,直接上用法。(纯干货)
主要用于账密token等轻量级存储。
特别感谢李佩朝童鞋的帮助(下面的工具类就是引用他的,这里不知道他有没有博客,就直接写名字了)
一:工具类及方法介绍:
/**
* SharedPreferences:共享偏好,用来做数据存储,通过xml,存放标记性数据和设置信息
*/
public class SharedPreferencesUtil {
//文件名称为config
private static final String PREFERENCE_NAME = "config";
//可以在此定义常亮,当做key使用
//版本号
public static final String APK_VERSION = "APK_VERSION";
//下载地址
public static final String APK_DOWNLOAD_URL = "APK_DOWNLOAD_URL";
private static SharedPreferences sharedPreferences;
/**
* 写入Boolean变量至sharedPreferences中
*
* @param context 上下文环境
* @param key 存储节点名称
* @param value 存储节点的值
*/
public static void putBoolean(Context context, String key, boolean value) {
//(存储节点文件名称,读写方式)
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
}
sharedPreferences.edit().putBoolean(key, value).commit();
}
/**
* 读取boolean标识从sharedPreferences中
*
* @param context 上下文环境
* @param key 存储节点名称
* @param value 没有此节点的默认值
* @return 默认值或者此节点读取到的结果
*/
public static boolean getBoolean(Context context, String key, boolean value) {
//(存储节点文件名称,读写方式)
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
}
return sharedPreferences.getBoolean(key, value);
}
/**
* 写入String变量至sharedPreferences中
*
* @param context 上下文环境
* @param key 存储节点名称
* @param value 存储节点的值String
*/
public static void putString(Context context, String key, String value) {
//存储节点文件的名称,读写方式
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
}
sharedPreferences.edit().putString(key, value).commit();
}
/**
* 读取String标识从sharedPreferences中
*
* @param context 上下文环境
* @param key 存储节点名称
* @param defValue 没有此节点的默认值
* @return 返回默认值或者此节点读取到的结果
*/
public static String getString(Context context, String key, String defValue) {
//存储节点文件的名称,读写方式
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
}
return sharedPreferences.getString(key, defValue);
}
/**
* 写入int变量至sharedPreferences中
*
* @param context 上下文环境
* @param key 存储节点名称
* @param value 存储节点的值String
*/
public static void putInt(Context context, String key, int value) {
//存储节点文件的名称,读写方式
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
}
sharedPreferences.edit().putInt(key, value).commit();
}
/**
* 读取int标识从sharedPreferences中
*
* @param context 上下文环境
* @param key 存储节点名称
* @param defValue 没有此节点的默认值
* @return 返回默认值或者此节点读取到的结果
*/
public static int getInt(Context context, String key, int defValue) {
//存储节点文件的名称,读写方式
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
}
return sharedPreferences.getInt(key, defValue);
}
/**
* 从sharedPreferences中移除指定节点
*
* @param context 上下文环境
* @param key 需要移除节点的名称
*/
public static void remove(Context context, String key) {
//存储节点文件的名称,读写方式
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
}
sharedPreferences.edit().remove(key).commit();
}
}
注释1:这里的文件名称可以随意写。
注释2:
这里的putBoolean/getBoolean及工具类中的putString/getString等表示的是 存储值和获取值。
注释3:
大家看这个remove也应该能知道,这里是清除数据了。
注释4:commit和apply区别
commit是同步缓存,将数据同步写入的磁盘和内存缓存;而apply则会将数据同步写入到内存缓存,然后异步保存到磁盘,这个过程可能会执行失败,而且执行失败之后没有返回失败回调信息。
二:工具类的用法:
1:作用域是整个app(如果是组件化的app建议放到common组件里边,可以作用域整个app)
2:一般在登录请求成功以后用:
//存储数据
SharedPreferencesUtil.putString(mContext,"name",loginBean.getUsername());
SharedPreferencesUtil.putString(mContext,"pwd",loginBean.getPassword());
SharedPreferencesUtil.putString(mContext,"token",loginBean.getToken());
3: 调用数据:(哪个页面都可以使用,直接 工具类名打点调用方法)
String name = SharedPreferencesUtil.getString(mContext,"name","");//取数据
String pwd = SharedPreferencesUtil.getString(mContext,"pwd","");//取数据
String token = SharedPreferencesUtil.getString(mContext,"token","");//取数据
转载请注明出处,谢谢大家。
我的CSDN:https://blog.csdn.net/u011208447?spm=1001.2101.3001.5113