Android使用SharedPreferences来缓存数据

导读:本篇文章讲解 Android使用SharedPreferences来缓存数据,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1.0存储类

/*
  * 缓存数据
  * 用之前要实例化
  * **/
public class CachePut
{
    public void putCacheInterG(Context context, String spName, String key, int value) {
        putCache(context, spName, key, value);
    }

    public void putCacheStringG(Context context, String spName, String key, String value) {
        putCache(context, spName, key, value);
    }

    public void putCacheBooleanG(Context context, String spName, String key, Boolean value) {
        putCache(context, spName, key, value);
    }

    public void putCacheLongG(Context context, String spName, String key, long value) {
        putCache(context, spName, key, value);
    }

    private static void putCache(Context context, String spName, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(spName, 0);
        sp.edit().putString(key, value).commit();
    }

    public static void putCache(Context context, String spName, String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(spName, 0);
        sp.edit().putInt(key, value).commit();
    }

    public static void putCache(Context context, String spName, String key, Boolean value) {
        SharedPreferences sp = context.getSharedPreferences(spName, 0);
        sp.edit().putBoolean(key, value.booleanValue()).commit();
    }

    public static void putCache(Context context, String spName, String key, long value) {
        SharedPreferences sp = context.getSharedPreferences(spName, 0);
        sp.edit().putLong(key, value).commit();
    }
}

2. 获取数据

/*
   * 获取缓存数据
   * 用之前要实例化
   * **/
public class CacheGet{
    public int getCacheIntegerG(Context context, String cacheName, String key) {
        return getCacheInteger(context, cacheName, key);
    }

    public String getCacheStringG(Context context, String cacheName, String key) {
        return getCacheString(context, cacheName, key);
    }

    public long getCacheLongG(Context context, String cacheName, String key) {
        return getCacheLong(context, cacheName, key);
    }

    public boolean getCacheBooleanG(Context context, String cacheName, String key) {
        return getCacheBoolean(context, cacheName, key);
    }

    private int getCacheInteger(Context context, String cacheName, String key) {
        SharedPreferences sp = context.getSharedPreferences(cacheName, 0);
        int receive = sp.getInt(key, 0);
        return receive;
    }

    private static String getCacheString(Context context, String cacheName, String key) {
        SharedPreferences sp = context.getSharedPreferences(cacheName, 0);
        String receive = sp.getString(key, “”);
        return receive;
    }

    private long getCacheLong(Context context, String cacheName, String key) {
        SharedPreferences sp = context.getSharedPreferences(cacheName, 0);
        long receive = sp.getLong(key, 0L);
        return receive;
    }

    private boolean getCacheBoolean(Context context, String cacheName, String key) {
        SharedPreferences sp = context.getSharedPreferences(cacheName, 0);
        boolean receive = sp.getBoolean(key, false);
        return receive;
    }
}

3.Activity中使用

      //实例化

        private CachePut cachePut  = new CachePut();

        //缓存激活信息
cachePut.putCacheStringG(mContext, Configs.CACHE_NAME, Configs.CACHE_BEGIN_TIME_KEY, begin_time);
cachePut.putCacheStringG(mContext, Configs.CACHE_NAME, Configs.CACHE_CURRENT_TIME_KEY, currentTime);
cachePut.putCacheStringG(mContext, Configs.CACHE_NAME, Configs.CACHE_END_TIME_KEY, end_time);

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119227.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!