Android SharedPreferences 数据缓存与获取?

一、创建缓存类CachePut.java

package com.etongwl.idsphoto.utils;

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

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();
    }
}

二、创建获取类CacheGet.java

package com.etongwl.idsphoto.utils;

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

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;
    }
}


三、在Activity中使用方式

1.首先声明

   CachePut    cachePut = new CachePut();
   CacheGet   cacheGet = new CacheGet();

存储String数据:cachePut.putCacheStringG(context, spName, key, value)

获取存储的String数据:cacheGet.getCacheStringG(context ,spName ,key)

同理可以对Int 、Long、Boolean等数据进行存储
 

以下是几个 Android 缓存数据的示例: 1. Shared Preferences: ```java // 保存数据SharedPreferences SharedPreferences.Editor editor = getSharedPreferences("my_prefs", MODE_PRIVATE).edit(); editor.putString("key", "value"); editor.apply(); // 从 SharedPreferences获取数据 SharedPreferences preferences = getSharedPreferences("my_prefs", MODE_PRIVATE); String value = preferences.getString("key", ""); ``` 2. SQLite 数据库: ```java // 创建 SQLiteOpenHelper 对象 SQLiteOpenHelper dbHelper = new MyDatabaseHelper(context, "my_database", null, 1); // 保存数据到 SQLite 数据库 SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", "John"); values.put("age", 25); db.insert("user", null, values); // 从 SQLite 数据库中获取数据 SQLiteDatabase db = dbHelper.getReadableDatabase(); String[] columns = {"name", "age"}; Cursor cursor = db.query("user", columns, null, null, null, null, null); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); } cursor.close(); ``` 3. 文件缓存: ```java // 保存数据到文件缓存 String data = "Hello, world!"; File cacheDir = context.getCacheDir(); File cacheFile = new File(cacheDir, "data.txt"); BufferedWriter writer = new BufferedWriter(new FileWriter(cacheFile)); writer.write(data); writer.close(); // 从文件缓存获取数据 File cacheDir = context.getCacheDir(); File cacheFile = new File(cacheDir, "data.txt"); BufferedReader reader = new BufferedReader(new FileReader(cacheFile)); String data = ""; String line; while ((line = reader.readLine()) != null) { data += line; } reader.close(); ``` 4. 内存缓存: ```java // 使用 LruCache 进行内存缓存 LruCache<String, String> cache = new LruCache<>(10); cache.put("key", "value"); String value = cache.get("key"); // 使用其他缓存框架进行内存缓存,如 Glide、Volley 等 Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView); ``` 以上示例仅供参考,实际使用时需要根据具体需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyxhzdm

你的鼓励是我创作的最大动力!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值