对SharePreference的封装
2024-10-10 05:52:13
今天需要用到SharePreference来保存一些设置参数,因为要用到很多次 所以对它进行了封装:
public class PrefUtils { public static void putBoolean(String key, boolean value, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().putBoolean(key, value).commit();
} public static boolean getBoolean(String key, boolean defValue, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
return sp.getBoolean(key, defValue);
} public static void putString(String key, String value, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().putString(key, value).commit();
} public static String getString(String key, String defValue, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
return sp.getString(key, defValue);
} public static void putInt(String key, int value, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().putInt(key, value).commit();
} public static int getInt(String key, int defValue, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
return sp.getInt(key, defValue);
} public static void remove(String key, Context ctx) {
SharedPreferences sp = ctx.getSharedPreferences("config",
Context.MODE_PRIVATE);
sp.edit().remove(key).commit();
}
}
最新文章
- XML简介与CDATA解释
- 移动手机端H5无缝间歇平滑向上滚动js代码
- install sun java in ubuntu
- 51nod平均数
- golang channel buffer
- shell 内网主机存活探测器
- WSO2 Stratos手册
- httpclient 302 重定向
- 【转】 iOS学习之sqlite的创建数据库,表,插入查看数据
- hadoop1.2.1+hbase0.90.4+nutch2.2.1+elasticsearch0.90.5配置(伪分布式)
- Linux系统挂载操作mount详解
- Golang源码探索(三) GC的实现原理
- PHPstorm 2017激活
- shell脚本修改文本中匹配行之前的行的方法
- python问题:AttributeError: 'module' object has no attribute 'SSL_ST_INIT'(转)
- C# 深浅复制 MemberwiseClone
- maven名词解释
- Contiki源码+原理+功能+编程+移植+驱动+网络(转)
- loadrunner12: Error -27492: ";HttpSendRequest"; failed, Windows error code=8
- 使用openssl生成双向加密证书(转)
热门文章
- iOS开发~UI布局(二)storyboard中autolayout和size class的使用详解
- 真机测试时的错误:No matching provisioning profiles found
- IOS项目集成ShareSDK实现第三方登录、分享、关注等功能。
- 使用CocoaPods管理第三方开源类库
- XCode升级到7后,规范注释生成器VVDocumenter插件没有用了,怎么办?
- [转]Linux下的Makefile
- python sorted
- Effective Java 14 In public classes, use accessor methods, not public fields
- Go对OO的选择
- 关于统计变换(CT/MCT/RMCT)算法的学习和实现