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的封装(数据持久化方式一)的更多相关文章

  1. iOS中的数据持久化方式

    iOS中的数据持久化方式,基本上有以下四种:属性列表.对象归档.SQLite3和Core Data. 1.属性列表 涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults ...

  2. iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】

                   在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...

  3. 四种数据持久化方式(下) :SQLite3 和 Core Data

    在上文,我们介绍了iOS开发中的其中2种数据持久化方式:属性列表.归档解档. 本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运用: 在本节,将通过对4个文 ...

  4. iOS数据持久化方式及class_copyIvarList与class_copyPropertyList的区别

    iOS数据持久化方式:plist文件(属性列表)preference(偏好设置)NSKeyedArchiver(归档)SQLite3CoreData沙盒:iOS程序默认情况下只能访问自己的程序目录,这 ...

  5. iOS开发中的4种数据持久化方式【一、属性列表与归档解档】

    iOS中的永久存储,也就是在关机重新启动设备,或者关闭应用时,不会丢失数据.在实际开发应用时,往往需要持久存储数据的,这样用户才能在对应用进行操作后,再次启动能看到自己更改的结果与痕迹.ios开发中, ...

  6. objective C中数据持久化方式1--对象归档

    第一.数据持久化的方式: NSKeyedArchiver--对象归档 属性列表化(NSArray.NSDictionary.NSUserDefault) SQlite数据库.CoreData数据库 其 ...

  7. iOS -数据持久化方式-以真实项目讲解

    前面已经讲解了SQLite,FMDB以及CoreData的基本操作和代码讲解(CoreData也在不断学习中,上篇博客也会不断更新中).本篇我们将讲述在实际开发中,所使用的iOS数据持久化的方式以及怎 ...

  8. Redis的两种数据持久化方式比较

    RDB(Redis Database) 本质:基于时间点的快照 优点: 1.RDB格式文件体积小. 2.可以通过脚本执行bgsave(非阻塞)或者save(阻塞)命令自定义时间点进行备份. 3.可以保 ...

  9. redis内存数据的持久化方式

    转: http://blog.csdn.net/wzqzhq/article/details/64920996 概述 Redis的强大性能很大程度上都是因为所有数据都是存储在内存中的,然而当Redis ...

随机推荐

  1. OPENCV3——从入门到出门

    跑第一个程序的时候经过坑爹的各种设置终于能用了. 如果遇到问题就谷歌或者百度,大牛的博客会给出解决方案的. vs2010+opencv3 目标:把书上的程序挨个敲一遍跑一遍. 现在已经跑了七章了,还有 ...

  2. 十二、VueJs 填坑日记之项目打包发布

    通过上一篇博文的学习,我们其实已经完成了我们设想的项目的开发.但是,我们做好的这套东西,是基于 nodejs 开发的.而我们最终希望,我们开发的项目,生成好一堆文件,然后随便通过任何一个 http 服 ...

  3. Redis在本地测试没有问题,上传的服务器后出现错误

    在服务器上,new Redis 可以拿到对象数据,但是其他操作就会报错. Redis 开启过程中,遇到错误 . :( protocol error, got 'S' as reply type byt ...

  4. 浅析php命名空间

    介绍 印象中只有java代码才会用到一大堆的import,当初看到后一脸懵逼并对php心生自豪:还是我大php牛逼够简洁,殊不知php也有命名空间这一说,这些年用的越来越多.那么,为什么要搞那么麻烦呢 ...

  5. Android UsageStatsService(应用使用统计服务)的学习与调研

    一. 简介 UsageStatsService是一个系统服务,其主要通过AMS等,来监测并记录各个应用的使用数据,如上次调用com.android.settings的时间等. UsageStatsSe ...

  6. 永中DCS再添喜讯:顺利签约海信集团

    近日,永中DCS与海信集团一起携手,共创文档在线预览新篇章.出于对永中DCS文档在线预览产品的品质与服务的信赖,海信集团选择永中DCS为其提供文档在线预览技术支持,助力移动化办公(EHR系统)发展,提 ...

  7. 利用阿里云Centos7建站过程

    以下可能不尽详述,如有问题欢迎指出 准备过程:1. 阿里云主机一台2.域名一个 3.github个人帐号开始: 1.以root帐号登录云主机 2.安装apache [root@192 ~]# yum ...

  8. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  9. php银联网页支付实现方法

         本文实例讲述了php银联网页支付实现方法.分享给大家供大家参考.具体分析如下: 这里介绍的银联WAP支付功能,仅限消费功能. 1. PHP代码如下: 复制代码代码如下: <?phpna ...

  10. python中csv文件的读取问题

    在python读取csv格式的文件时,使用csv.reader读取文件对象,出现了line contains NULL byte的错误,如下: reader = csv.reader(open(fil ...