SP的封装(数据持久化方式一)
1.先看一段描述:
Interface for accessing and modifying preference data returned by Context.getSharedPreferences(java.lang.String, int). For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through an SharedPreferences.Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage
这段描述讲的是sp(SharedPreferences实例)获取方式:Context.getSharedPreferences(java.lang.String, int),这个方法有两个参数,第一个表示sp对应xml的文件名,第二个为这个文件的模式,私有,共有,可读,可写
修改sp需要使用编辑器:SharedPreferences.Editor,然修改后要提交:edit.commit
2.这个sp实例为整个应用程序共享,从sp维护的xml文件中,可以存取各种类型数据
3.先看看我封装SP工具类的代码,这个封装经过测试,可以很方便使用sp存取数据
package com.market.sp; import android.content.Context;
import android.content.SharedPreferences; import java.util.Set; import static android.R.id.edit; /**
* 对SharedPreference的封装
* 在包名目录下创建一个shared_pres目录,并维护一个config.xml文件
* 所有数据的读取和存入都是对这个文件的操作
* Created by Administrator on 2017/6/15.
*/ public class SPUtils { private static SharedPreferences sp = null; /**
* 将一个boolean值存入sp文件中
* @param ctx 上下文
* @param key 存储节点名称
* @param value 存储节点的值
*/
public static void putBoolean(Context ctx, String key, boolean value){
//如果sp为空,则获取创建一个sp对象
if(sp == null){
sp = ctx.getSharedPreferences("config",Context.MODE_PRIVATE);
}
sp.edit().putBoolean(key,value).commit();//获取sp编辑器,放入bool值,并提交 } /**
* 根据key读取一个boolean值value,没有的话使用defvalue代替
* @param ctx
* @param key
* @param defvalue
*/
public static boolean getBoolean(Context ctx, String key, boolean defvalue){
//如果sp为空,则获取创建一个sp对象
if(sp == null){
sp = ctx.getSharedPreferences("config",Context.MODE_PRIVATE);
}
boolean b = sp.getBoolean(key, defvalue);
return b; } /**
* 将一个String值存入sp文件中
* @param context 上下文
* @param key 存储节点名称
* @param value 存储节点的值
*/
public static void putString(Context context,String key,String value){
if(sp == null){//如果sp文件不存在,则创建该文件
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
sp.edit().putString(key, value).commit(); }
/**
* 从sp中根据key取出String值
* @param context 上下文
* @param key 存储节点名称
* @param defValue 存储节点默认值
* @return string
*/
public static String getString(Context context,String key,String defValue){
if(sp == null){//如果sp文件不存在,则创建该文件
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
String string = sp.getString(key, defValue);
return string; }
/**
* 移除sp中的一个节点
* @param context 上下文环境
* @param key 节点名称
*/
public static void removeFromSP(Context context, String key) {
if(sp == null){//如果sp文件不存在,则创建该文件
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
final SharedPreferences.Editor edit = sp.edit();
edit.remove(key); }
/**
* 从sp中根据key取出int值
* @param context
* @param key
* @param defValue
* @return
*/
public static int getInt(Context context, String key, int defValue) {
if(sp == null){//如果sp文件不存在,则创建该文件
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
int i = sp.getInt(key, defValue);
return i; }
/**
* 将一个int值存入sp文件中
* @param context
* @param key
* @param value
*/
public static void putInt(Context context,String key,int value){
if(sp == null){//如果sp文件不存在,则创建该文件
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
sp.edit().putInt(key, value).commit(); } /**
* 从sp中根据key取出float值
* @param context
* @param key
* @param defValue
* @return
*/
public static float getFloat(Context context, String key, float defValue) {
if(sp == null){//如果sp文件不存在,则创建该文件
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
float i = sp.getFloat(key, defValue);
return i; }
/**
* 将一个float值存入sp文件中
* @param context
* @param key
* @param value
*/
public static void putFloat(Context context,String key,float value){
if(sp == null){//如果sp文件不存在,则创建该文件
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
sp.edit().putFloat(key,value).commit(); } /**
* 从sp中根据key取出int值
* @param context
* @param key
* @param defValue
* @return
*/
public static Set<String> getStringSet(Context context, String key, Set<String> defValue) {
if(sp == null){//如果sp文件不存在,则创建该文件
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
Set<String> sets = sp.getStringSet(key, defValue);
return sets; }
/**
* 将一个int值存入sp文件中
* @param context
* @param key
* @param sets
*/
public static void putStringSet(Context context,String key,Set<String> sets){
if(sp == null){//如果sp文件不存在,则创建该文件
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
sp.edit().putStringSet(key,sets).commit(); } }
4.测试代码:
package com.market.sp; import android.app.Activity;
import android.os.Bundle;
import android.util.Log; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SPUtils.putBoolean(this,"ismy",true);
SPUtils.putFloat(this,"myfloat",23.45f); Log.e(getLocalClassName(),SPUtils.getBoolean(this,"ismy",false)+"");
Log.e(getLocalClassName(),SPUtils.getFloat(this,"myfloat",0.0f)+"");
}
5.运行效果展示
打印结果

文件结果:可以看到在/data/data/com.market.sp/生成了shared_prefs目录,且在该目录下生成config.xml文件

文件内容查看:可见存储了两个节点float和boolean,而且我们要存储的数据都在里面

SP的封装(数据持久化方式一)的更多相关文章
- iOS中的数据持久化方式
iOS中的数据持久化方式,基本上有以下四种:属性列表.对象归档.SQLite3和Core Data. 1.属性列表 涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults ...
- iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】
在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...
- 四种数据持久化方式(下) :SQLite3 和 Core Data
在上文,我们介绍了iOS开发中的其中2种数据持久化方式:属性列表.归档解档. 本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运用: 在本节,将通过对4个文 ...
- iOS数据持久化方式及class_copyIvarList与class_copyPropertyList的区别
iOS数据持久化方式:plist文件(属性列表)preference(偏好设置)NSKeyedArchiver(归档)SQLite3CoreData沙盒:iOS程序默认情况下只能访问自己的程序目录,这 ...
- iOS开发中的4种数据持久化方式【一、属性列表与归档解档】
iOS中的永久存储,也就是在关机重新启动设备,或者关闭应用时,不会丢失数据.在实际开发应用时,往往需要持久存储数据的,这样用户才能在对应用进行操作后,再次启动能看到自己更改的结果与痕迹.ios开发中, ...
- objective C中数据持久化方式1--对象归档
第一.数据持久化的方式: NSKeyedArchiver--对象归档 属性列表化(NSArray.NSDictionary.NSUserDefault) SQlite数据库.CoreData数据库 其 ...
- iOS -数据持久化方式-以真实项目讲解
前面已经讲解了SQLite,FMDB以及CoreData的基本操作和代码讲解(CoreData也在不断学习中,上篇博客也会不断更新中).本篇我们将讲述在实际开发中,所使用的iOS数据持久化的方式以及怎 ...
- Redis的两种数据持久化方式比较
RDB(Redis Database) 本质:基于时间点的快照 优点: 1.RDB格式文件体积小. 2.可以通过脚本执行bgsave(非阻塞)或者save(阻塞)命令自定义时间点进行备份. 3.可以保 ...
- redis内存数据的持久化方式
转: http://blog.csdn.net/wzqzhq/article/details/64920996 概述 Redis的强大性能很大程度上都是因为所有数据都是存储在内存中的,然而当Redis ...
随机推荐
- Azure 基础 : 使用 Automation 定时开机
不知何时 Azure 为虚机提供了自动关机的功能.这是一个很棒的功能,可以帮助我们定时关闭虚机并释放掉资源以节省开支.如果某台虚机在夜间不需要提供服务,我们就可以把它配置为晚上的某个时间点自动关机: ...
- Android自定义指示器时间轴
指示器时间轴在外卖.购物类的APP里会经常用到,效果大概就像下面这样,看了网上很多文章,大都是自己绘制,太麻烦,其实通过ListView就可以实现. 在Activity关联的布局文件activit ...
- 虚拟机创建流程中neutron代码分析(二)
前言: 当nova服务发送了创建port的restful调用信息之后,在neutron服务中有相应的处理函数来处理调用.根据restful的工作原理,是按照 paste.ini文件中配置好的流程去处理 ...
- 《Linux命令行与shell脚本编程大全》第二十二章 gawk进阶
gawk是一门功能丰富的编程语言,你可以通过它所提供的各种特性来编写好几程序处理数据. 22.1 使用变量 gawk编程语言支持两种不同类型的变量: 内建变量和自定义变量 22.1.1 内建变量 ga ...
- 自己动手实现mvc框架
用过springmvc的可能都知道,要集成springmvc需要在web.xml中加入一个跟随web容器启动的DispatcherServlet,然后由该servlet初始化一些东西,然后所有的web ...
- 51Nod--1008
1008 N的阶乘 mod P 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 输入N和P(P为质数),求N! Mod P = ? (Mod 就是求模 % ...
- macbookPro 搭建maven环境下载jar包
今天要用itext的jar包,去官网看发现好像只能用maven下载,而我之前又换了电话,没办法咯,重新搭一次maven环境吧,在此记录,已便分享或自己将来查找 首选确定自己环境上jdk装好了,如果没有 ...
- Codeforces 482B Interesting Array
题意:构造一个长度为n的序列,使其满足m个形式如下如下约束:a[l]&a[l+1]&a[l+2]&....&a[r]=q 从Dalao的博客上看到这题,决定去水水.做法 ...
- CF Round#436 div2
额,这次的题目其实挺智障的.所以通过这次比赛,我也发现了自己是一个智障.... 不说太多,说多是泪... A. Fair Game 题意:给你一个数组,看你能否把它均分为两个所有元素均相同的子数组. ...
- DOM操作整理
DOM获取 1. 直接获取 document.getElementById("box_id") 通过ID获取 document.getElementsByName("my ...