自定义缓存工具类----sharedPreferences

          大家好,今晚我给大家带来非常实用的自定义工具类sharedPreferences,我把它放在一个专题来讲,主要是因为这个技术在开发当中用的相当多,而我把它封装为一个类,抽取出来,等需要用的时候,直接拿出来,方便快捷,效果也正佳。我们在登录界面,首次进入的欢迎界面,第二次就不再出现欢迎界面,还有勾选复选框等方面可以使用到。


我首先创建一个工具类CacheUtils.java,封装方法,里面的方法包括存入数据,获取数据,初始化sharedPreferences方法。代码如下:

/**
 *缓存工具类
 */
public class CacheUtil {
 //sharedPreferences储存文件名,在data/data/<包名>/shared-frefs/目录下,文件格式是xml
 public static final String CONFIG_SP = "config_sp";

//是否第一次使用,is_first_user是第一条数据名
 public static final String IS_FIRST_ENTER = "is_first_user";

//是否要更新,apk_udate是第二天数据名
 public static final String APK_UPDATE = "apk_update";
 private static SharedPreferences mSp;
    
 //构建初始化方法
 private static SharedPreferences getPreferences(Context context) {
  if (mSp == null) {

//参数一:xml文件名,参数二:存储状态
   mSp = context.getSharedPreferences(CONFIG_SP, context.MODE_PRIVATE);
  }
  return mSp;

 }

//保存布尔数据,参数一:上下文,参数二:key值,参数三:value值
 public static void putBlooean(Context context, String key, boolean value) {
  //调用getPreferences方法初始化
  SharedPreferences sp = getPreferences(context);

//编辑器
  Editor editor = sp.edit();

//保存数据,根据key,value
  editor.putBoolean(key, value);
  //提交
  editor.commit();

 };

//读取布尔数据,有返回值,返回数据,根据key读取
 public static boolean getBlooean(Context context, String key) {
  SharedPreferences sp = getPreferences(context);
  //返回数据,参数二:默认值,
  return sp.getBoolean(key, false);
 }

 //读取布尔数据,返回的是默认值,这种情况通常使用在第一次判断key值,当key值没有时,就能用到默认值
 public static boolean getBlooean(Context context, String key, boolean defvalue) {
  SharedPreferences sp = getPreferences(context);
  //返回默认值
  return sp.getBoolean(key, defvalue);
 }

 
 //保存String数据
  public static void putString(Context context, String key, String value) {
   SharedPreferences sp = getPreferences(context);
   Editor editor = sp.edit();
   editor.putString(key, value);
  //提交
   editor.commit();

  };

//读取数据,有返回值,在key有值下条件成立
  public static String getString(Context context, String key) {
   SharedPreferences sp = getPreferences(context);
   return sp.getString(key, null);
  }

  //读取数据返回默认值
  public static String getString(Context context, String key, String defvalue) {
   SharedPreferences sp = getPreferences(context);
   return sp.getString(key, defvalue);
  }
 
}

下面以一个简单的例子解释,我是截取一小段代码讲解,之间还有很多操作没讲,专门解释自定义缓存的作用

//判断,是否能得到key值CacheUtil.ENTER_LOSTFIND,默认值是false,

//当它第一进入时,可以肯定的是还没key值,和value值,它还没有保存value值,所有就会根据默认值,执行else语句,进入Setup1Activity页面,

//而当她能获取value值,它就会执行key成功,不会执行默认值false,从而进入Lost_find界面
  if(CacheUtil.getBlooean(getApplicationContext(), CacheUtil.ENTER_LOSTFIND,false)){
   setContentView(R.layout.activity_lost_find);
  //第一次进入到导航页面
  }else{
   Intent intent=new Intent(LostFindActivity.this,Setup1Activity.class);
   startActivity(intent);
  }


 //这一步是提交数据,要理解的是有提交才key,这就明白第一次为什么没获得key了,因为它都没提交生成key
 public void next(View v) {
  Intent intent = new Intent(Setup4Activity.this, LostFindActivity.class);
//提交缓存到Config.xml文件,把value改为true,之前是没有value值,能进到这一步,因为设置默认值false
  CacheUtil.putBlooean(context, CacheUtil.ENTER_LOSTFIND, true);
  ToastUtil.show(context, "缓存成功");
  startActivity(intent);
   finish();
 }

好用方便的自定义缓存工具类就到这里了,现在你可以体会不多它的好用在什么地方,等你在开发中,你就会感叹,这太好用了,马上爱不释手。晚安了~




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值