博客
关于我
2.Android篇——利用SharedPreferences封装本地存储工具类
阅读量:486 次
发布时间:2019-03-07

本文共 2323 字,大约阅读时间需要 7 分钟。

问题:

有的时候我们一个查询的接口需要重复的请求多次,尽管每次返回的结果是一样的,但是还是需要请求多次,这个时候我们就可以再第一次请求的时候把数据存到我们的缓存中,之后的请求就可以直接调用缓存中没有过期的数据,这里我们奉上工具类代码:

package com.zjxf.utils;import android.content.SharedPreferences;public class SharedPrefUtils {	private static SharedPrefUtils instance;	private SharedPreferences sp;	private SharedPrefUtils() {		sp = BDApplication.getInstance().getSharedPreferences(				BDApplication.getInstance().getPackageName(), 0);	}	public static SharedPrefUtils getInstance() {		if (instance == null) {			synchronized (SharedPrefUtils.class) {				if (instance == null) {					instance = new SharedPrefUtils();				}			}		}		return instance;	}	public void putStr2SP(String key, String value) {		sp.edit().putString(key, value).commit();	}	public String getStrBykey(String key, String defValue) {		return sp.getString(key, defValue);	}	public void putInt2SP(String key, Integer value) {		sp.edit().putInt(key, value).commit();	}	public Integer getIntByKey(String key, Integer defValue) {		return sp.getInt(key, defValue);	}	public void putFloat2SP(String key, Float value) {		sp.edit().putFloat(key, value).commit();	}	public Float getFloatByKey(String key, Float defValue) {		return sp.getFloat(key, defValue);	}	public void putLong2SP(String key, Long value) {		sp.edit().putLong(key, value).commit();	}	public Long getLongByKey(String key, Long defValue) {		return sp.getLong(key, defValue);	}		public void putBoolean2SP(String key, boolean value) {		sp.edit().putBoolean(key, value).commit();	}		public Boolean getBooleanByKey(String key, boolean defValue) {		return sp.getBoolean(key, defValue);	}	public void remove(String key) {		try {			sp.edit().remove(key).commit();		} catch (Exception e) {			e.printStackTrace();		}	}}
package com.zjxf.utils;import android.app.Application;public class BDApplication extends Application {	private static BDApplication instance;		@Override	public void onCreate() {		super.onCreate();		instance = this;		CrashHandler.getInstance().init(this.getApplicationContext());	}		public static BDApplication getInstance(){		return instance;	}}

上面这个全局的类需要在AndroidManifest.xml里面设置一下

然后就可以使用了,用法如下:

SharedPrefUtils.getInstance().putStr2SP(CacheKeys.LOGIN_SESSION_Id, sessionId); //添加值 String schoolName = SharedPrefUtils.getInstance().getStrBykey(CacheKeys.THIS_SCHOOL_NAME, StringUtil.EMPTY_STRING); //获取值

 

转载地址:http://iaucz.baihongyu.com/

你可能感兴趣的文章
Nginx 学习总结(16)—— 动静分离、压缩、缓存、黑白名单、性能等内容温习
查看>>
Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
查看>>
Nginx 学习(一):Nginx 下载和启动
查看>>
nginx 常用指令配置总结
查看>>
Nginx 常用配置清单
查看>>
nginx 常用配置记录
查看>>
nginx 开启ssl模块 [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx
查看>>
Nginx 我们必须知道的那些事
查看>>
Nginx 源码完全注释(11)ngx_spinlock
查看>>
Nginx 的 proxy_pass 使用简介
查看>>
Nginx 的配置文件中的 keepalive 介绍
查看>>
Nginx 结合 consul 实现动态负载均衡
查看>>
Nginx 负载均衡与权重配置解析
查看>>
Nginx 负载均衡详解
查看>>
nginx 配置 单页面应用的解决方案
查看>>
nginx 配置https(一)—— 自签名证书
查看>>
nginx 配置~~~本身就是一个静态资源的服务器
查看>>
Nginx 配置清单(一篇够用)
查看>>
Nginx 配置解析:从基础到高级应用指南
查看>>
nginx+php的搭建
查看>>