自定义简单的封装SharedPreferences缓存类

首先介绍一下什么是SharedPreferences.
SharedPreferences是Android中最容易理解的数据存储技术,是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,由于里面存放的数据不多,因此采用数据库存放并不划算,因此使用键值对这种一一对应的关系来存放这些信息,SharedPreferences正是实现了这种存储方式的技术.
SharedPreferences是一个接口,获取其对象的方式有两种:
1.调用Activity对象的getPreferences()方法
2.调用Context对象的getSharedPreferences()方法
这两种方式的区别是:
使用Activity对象获取的SharedPreferences对象只能在该Activity中使用;
使用Context对象获取的SharedPreferences对象可以在同一应用程序中共享

在SharedPreferences中存储的数据背后都是以XML文件形式存放的,
文件存放在/data/data/< package name>/shared_prefs目录下

SharedPreferences用于四种操作模式,分别是:
1.Context.MODE_PRIVATE
默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件内容
2.Context.MODE_APPEND
该模式会检查文件是否存在,存在就在原文件中追加内容,否则创建新文件
3.Context.MODE_WORLD_READABLE
4.Context.MODE_WORLD_WRITEABLE
这两种模式是用来控制其他应用是否具有权限读写该文件,一种是读取一种是写入

正常使用SharedPreferences保存key-value数据步骤如下:
1.获取SharedPreferences对象

SharedPreferences sp = context.getSharedPreferences("name", Context.MODE_PRIVATE);

参数1:存储key-value数据的文件名称,第二个参数是该文件的操作方式,我们这里选取默认操作方式,只能在该应用本身使用

2.使用SharedPreferences的edit()方法获得Edit对象来操作该文件

SharedPreferences.Editor editor = sp.edit();

Editor是SharedPreferences的内部接口,通过edit()方法可以返回一个Editor对象

3.通过Editor对象调用putXXX方法保存key-value形式的数据,可以保存不同的数据类型,如int,long,String等等.

editor.putInt("number", 12345);
editor.putString("userName", "张三");

4.通过SharedPreferences.Editor的commit()方法保存key-value,相当于将设置好的操作提交操作.

editor.commit();

通过上述四个步骤完成了存储key-value形式的轻量级数据

之后简单的演示一下如何获取存储在SharedPreferences中的数据

SharedPreferences sp = context.getSharedPreferences("name", Context.MODE_PRIVATE);
 // 参数2: 获取失败时返回一个默认值
 int num = sp.getInt("number", 0);
 String name =  sp.getString("userName", "失败");

通过上述操作可以简单的完成一次通过SharedPreferences存储和获取简单的数据,在实际应用中如果我们需要多次的利用SharedPreferences类来获取应用状态,那么每一次进行对象的获取比较麻烦,因此可以考虑将该类封装起来,每一次通过我们自己封装的类来调用方法存储和获取,不但方便,还实现类似于按类存储的形式,获取时可以按照某一类数据获取,相应的清除缓存也很方便.

下面看一下代码:

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 封装SharedPreferences类
 */
public class SharedConfig {

    /**
     * 存储数据到缓存
     * @param context 用来获取SharedPreferences对象
     * @param name 文件的名字
     * @param key 存储值的key
     * @param value 存储的值
     */
    public static void putSharedConfig(Context context, String name, String key, float floatValue) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        sp.edit().putFloat(key, floatValue).commit();
    }

    public static void putSharedConfig(Context context, String name, String key, boolean booleanValue){
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        sp.edit().putBoolean(key, booleanValue).commit();
    }

    public static void putSharedConfig(Context context, String name, String key, int intValue) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        sp.edit().putInt(key, intValue).commit();
    }

    public static void putSharedConfig(Context context, String name, String key, long longValue) {
        context.getSharedPreferences(name, Context.MODE_PRIVATE).edit().putLong(key, longValue).commit();
    }

    public static void putSharedConfig(Context context, String name, String key, String stringValue) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        sp.edit().putString(key, stringValue).commit();
    }

    /**
     * 获取存储在缓存中的值
     * @param context
     * @param name
     * @param key
     * @return
     */
    public static float getSharedConfigFloat(Context context, String name, String key) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        float returnValue = sp.getFloat(key, 0.0f);
        return returnValue;
    }
    public static boolean getSharedConfigBoolean(Context context, String name, String key) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        boolean returnValue = sp.getBoolean(key, false);
        return returnValue;
    }
    public static int getSharedConfigInt(Context context, String name, String key) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        int returnValue = sp.getInt(key, 0);
        return returnValue;
    }
    public static long getSharedConfigLong(Context context, String name, String key) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        long returnValue = sp.getLong(key, 0);
        return returnValue;
    }
    public static String getSharedConfigString(Context context, String name, String key) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        String returnValue = sp.getString(key, "取值失败");
        return returnValue;
    }

    /**
     * 清除该文件的内容
     * @param context
     * @param name 文件名
     */
    public static void clearConfig(Context context, String name) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        sp.edit().clear().commit();
    }

}

封装完这个类之后,演示一下如何使用:

  // 存储数据到SharedPreferences
  SharedConfig.putSharedConfig(context, "userInfo", "number", 001);
  SharedConfig.putSharedConfig(context, "userInfo", "userName", "李四");
  SharedConfig.putSharedConfig(context, "userInfo", "pas", 001002);

使用我们封装的类调用存储的静态方法,
参数1:Context对象
参数2:文件名
参数3:key
参数4:value

通过我们封装的类调用获取的静态方法

// 获取SharedPreferences存储的数据
 int num = SharedConfig.getSharedConfigInt(context, "userInfo", "number");
 String name = SharedConfig.getSharedConfigString(context, "userInfo", "userName");
 Log.d(TAG, "num:" + num); 

通过这个类的封装可以熟练掌握SharedPreferences的用法.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值