一、使用
1.写入不同类型:根据泛型
SpUtils<Boolean> spBoolean = new SpUtils<>(this);
spBoolean.put("Boolean", true);
SpUtils<Integer> spInteger = new SpUtils<>(this);
spInteger.put("Integer", 100);
SpUtils<Long> spLong = new SpUtils<>(this);
spLong.put("Long", new Date().getTime());
SpUtils<String> spString = new SpUtils<>(this);
spString.put("String", "I am toly");
SpUtils<Float> spFloat = new SpUtils<>(this);
spFloat.put("Float", 3.14f);2.读取已设置的值:第二参数是key不存在时,value的默认值
Boolean reslute = spBoolean.get("Boolean", false);sp.png
本文由张风捷特烈原创,转载请注明
更多安卓技术欢迎访问:https://www.jianshu.com/c/004f3fe34c94
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
你的喜欢与支持将是我最大的动力
附录一、封装类
/**
* 作者:张风捷特烈<br/>
* 时间:2018/2/20:17:57<br/>
* 邮箱:1981462002@qq.com<br/>
* 说明:SharedPreferences工具类 :默认apply方式提交<br/>
*/public class SpUtils<T> { /**
* 上下文
*/
private Context mContext; /**
* 是否以apply方式提交:否则是commit方式提交
*/
private boolean isApply = true; /**
* SharedPreferences对象
*/
private static SharedPreferences sp; /**
* 构造函数 传入上下文
*
* @param context 上下文
*/
public SpUtils(Context context) {
mContext = context;
} /**
* 写入至sp中
*
* @param key 存储节点名称
* @param value 存储节点的值 boolean
* @return 是否插入成功 apply 方式返回null
*/
public Boolean put(String key, T value) { //(存储节点文件名称,读写方式)
if (sp == null) {
sp = mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
}
SharedPreferences.Editor editor = null; if (value instanceof Boolean) {
editor = sp.edit().putBoolean(key, (Boolean) value);
} if (value instanceof String) {
editor = sp.edit().putString(key, (String) value);
} if (value instanceof Integer) {
editor = sp.edit().putInt(key, (Integer) value);
} if (value instanceof Long) {
editor = sp.edit().putLong(key, (Long) value);
} if (value instanceof Float) {
editor = sp.edit().putFloat(key, (Float) value);
} if (editor != null) { if (isApply) {
editor.apply(); return null;
} else { return editor.commit();
}
} return false;
} /**
* 从sp中读取
*
* @param key 存储节点名称
* @param defValue 默认值
* @return 读取boolean标示从sp中
*/
public T get(String key, T defValue) { //(存储节点文件名称,读写方式)
Object o = new Object(); if (sp == null) {
sp = mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
} if (defValue instanceof Boolean) {
o = (sp.getBoolean(key, (Boolean) defValue));
} if (defValue instanceof String) {
o = sp.getString(key, (String) defValue);
} if (defValue instanceof Integer) {
o = sp.getInt(key, (Integer) defValue);
} if (defValue instanceof Long) {
o = sp.getLong(key, (Long) defValue);
} if (defValue instanceof Float) {
o = sp.getFloat(key, (Float) defValue);
} return (T) o;
} /**
* 从sp中移除指定节点
*
* @param key 需要移除节点的名称
* @return 是否插入成功 apply 方式返回null
*/
public Boolean remove(String key) { if (sp == null) {
sp = mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
}
SharedPreferences.Editor editor = sp.edit().remove(key); if (isApply) {
editor.apply(); return null;
} else { return editor.commit();
}
}
/**
* 设置是否以 apply方式提交
* @param apply 以apply方式提交,否则:commit
*/
public void setApply(boolean apply) {
isApply = apply;
}
}附录2:apply和commit
相同点: 1.提交preference修改数据 2.是原子过程 区别: 1.apply没有返回值而commit返回boolean表明修改是否提交成功 2.apply是将修改数据原子提交到内存,而后异步真正提交到硬件磁盘; 而commit是同步的提交到硬件磁盘,因此,在多个并发的提交commit的时候, 他们会等待正在处理的commit保存到磁盘后在操作,从而降低了效率。 而apply只是原子的提交到内容,后面有调用apply的函数的将会直接覆盖前面的内存数据, 这样从一定程度上提高了很多效率。 3.apply方法不会提示任何失败的提示。 综合上述,由于在一个进程中,sharedPreference是单实例,一般不会出现并发冲突, 如果对提交的结果不关心的话,建议使用apply,当然需要确保提交成功且有后续操作的话,还是需要用commit的。
更新:使用工厂模式
使用方法:
//初始化:可在自定义Application里初始化SPFactory.init(getApplicationContext());//设置值SPFactory.getSP(DataType.INT).put("SP_DEFAULT_LIFE", 1000);
SPFactory.getSP().put("SP_DEFAULT_NAME", "Toly");
SPFactory.getSP(DataType.BOOLEAN).put("SP_MAN", false);
SPFactory.getSP(DataType.FLOAT).put("PI", 3.1415f);
SPFactory.getSP(DataType.LONG).put("SP_CURRENT_TIME", System.currentTimeMillis());//获取值String name = (String) SPFactory.getSP().get("SP_DEFAULT_NAME", "");//删除键值对SPFactory.getSP(DataType.BOOLEAN).remove("SP_MAN");//清楚配置SPFactory.getSP().clear();为了统一SpUtils的返回,这里定义了一个接口
/**
* 作者:张风捷特烈<br/>
* 时间:2018/10/28 0028:23:45<br/>
* 邮箱:1981462002@qq.com<br/>
* 说明:SpUtils的抽象接口
*/public interface Shareable<T> { Boolean put(String key, T value); T get(String key, T defValue); Boolean remove(String key); Boolean clear();
}数据类型枚举
/**
* 作者:张风捷特烈<br/>
* 时间:2018/10/29 0029:8:49<br/>
* 邮箱:1981462002@qq.com<br/>
* 说明:数据类型枚举
*/public enum DataType {
INT, FLOAT, LONG, BOOLEAN, STRING;
}SpUtils的生产工厂
/**
* 作者:张风捷特烈<br/>
* 时间:2018/10/28 0028:23:34<br/>
* 邮箱:1981462002@qq.com<br/>
* 说明:SpUtils的生产工厂
*/public class SPFactory { //这里的静态上下文最好用全局的
private static Context sContext; public static void init(Context context) {
sContext = context;
}
/**
* 一参,必须先初始化,推荐使用Application里通过全局上下文
*
* @param type 该类型的任意对象
* @return
*/
public static ISharedable getSP(DataType type) { return getSP(sContext, type, "config");
} /**
* 默认String类型
* @return
*/
public static ISharedable getSP() { return getSP(sContext, DataType.STRING, "config");
} //为提高性能,不重复new ,采用集合装载,以便复用
private static HashMap<DataType, ISharedable> mSpMap = new HashMap<>(); public static ISharedable getSP(Context context, DataType type, String fileName) {
ISharedable sharedable = mSpMap.get(type);//在映射中,复用
if (sharedable == null) { switch (type) { case INT:
sharedable = new SpUtils<Integer>(context, fileName); break; case LONG:
sharedable = new SpUtils<Long>(context, fileName); break; case FLOAT:
sharedable = new SpUtils<Float>(context, fileName); break; case STRING:
sharedable = new SpUtils<String>(context, fileName); break; case BOOLEAN:
sharedable = new SpUtils<Boolean>(context, fileName); break;
}
mSpMap.put(type, sharedable);//将新建的fragment加入集合中
} return sharedable;
} public static ISharedable getSP(Context context, DataType type) { return getSP(context, type, "config");
}
}
作者:张风捷特烈
链接:https://www.jianshu.com/p/0ecaea229c6e
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
