安卓临时缓存sp工具类

复制代码
支持对象存储(自动 JSON 序列化和反序列化)的 Android SPUtils 工具类,使用 Gson 实现对象与字符串的互转。
✅ Gson 依赖(如果尚未添加)
在 build.gradle 中加入:

implementation 'com.google.code.gson:gson:2.10.1'
✅ 支持对象的 SPUtils 工具类

package com.htnova.fly.util;

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

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

public class SPUtils {
    private static final String SP_NAME = "AppCache";
    private static SharedPreferences sp;
    private static Gson gson = new Gson();

    private SPUtils() {}

    public static void init(Context context) {
        if (sp == null) {
            sp = context.getApplicationContext().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        }
    }

    // 基础类型
    public static void putString(String key, String value) {
        sp.edit().putString(key, value).apply();
    }

    public static String getString(String key, String defValue) {
        return sp.getString(key, defValue);
    }

    public static void putBoolean(String key, boolean value) {
        sp.edit().putBoolean(key, value).apply();
    }

    public static boolean getBoolean(String key, boolean defValue) {
        return sp.getBoolean(key, defValue);
    }

    public static void putInt(String key, int value) {
        sp.edit().putInt(key, value).apply();
    }

    public static int getInt(String key, int defValue) {
        return sp.getInt(key, defValue);
    }

    public static void putLong(String key, long value) {
        sp.edit().putLong(key, value).apply();
    }

    public static long getLong(String key, long defValue) {
        return sp.getLong(key, defValue);
    }

    public static void putFloat(String key, float value) {
        sp.edit().putFloat(key, value).apply();
    }

    public static float getFloat(String key, float defValue) {
        return sp.getFloat(key, defValue);
    }

    // 存储任意对象
    public static <T> void putObject(String key, T obj) {
        String json = gson.toJson(obj);
        sp.edit().putString(key, json).apply();
    }

    // 获取对象
    public static <T> T getObject(String key, Class<T> clazz) {
        String json = sp.getString(key, null);
        return json != null ? gson.fromJson(json, clazz) : null;
    }

    // 获取集合或泛型对象(如 List<User>)
    public static <T> T getObject(String key, Type typeOfT) {
        String json = sp.getString(key, null);
        return json != null ? gson.fromJson(json, typeOfT) : null;
    }

    public static void remove(String key) {
        sp.edit().remove(key).apply();
    }

    public static void clear() {
        sp.edit().clear().apply();
    }

    public static boolean contains(String key) {
        return sp.contains(key);
    }
}
✅ 示例用法
1. 存储/读取单个对象:

User user = new User("Tom", 25);
SPUtils.putObject("user", user);

User savedUser = SPUtils.getObject("user", User.class);
2. 存储/读取 List 对象:

List<User> userList = new ArrayList<>();
userList.add(new User("Tom", 25));
userList.add(new User("Jerry", 26));

SPUtils.putObject("user_list", userList);

// 获取时需要指定 Type
Type type = new TypeToken<List<User>>() {}.getType();
List<User> savedList = SPUtils.getObject("user_list", type);
✅ 示例数据类(User)

public class User {
    private String name;
    private int age;

    public User() {}

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter / Setter 省略
}
相关推荐
alexhilton10 小时前
将应用迁移到Navigation 3:痛点、加班和紧急修复
android·kotlin·android jetpack
杉氧15 小时前
Navigation Compose 深度实践:如何优雅地串联起你的全栈 App?
android·架构·android jetpack
雨白19 小时前
指针与数组的核心机制
android
黄林晴1 天前
Room 3.0 正式发布!包名彻底重构,KMP 成为核心主线
android·android jetpack
三少爷的鞋1 天前
Kotlin 协程环境下的 DCL 懒加载:别把线程时代的经验直接搬过来
android
plainGeekDev1 天前
Gson → kotlinx.serialization
android·java·kotlin
CYY952 天前
Compose 入门篇
android·kotlin
杉氧2 天前
Compose 时代的 MVI 架构:如何用单向数据流驱动复杂 UI?
android·架构·android jetpack
杉氧2 天前
Modifier 的艺术:为什么链式调用的顺序决定了UI 的生命周期?
android·架构·android jetpack