支持对象存储(自动 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 省略
}
安卓临时缓存sp工具类
安卓理事人2025-12-01 10:54
相关推荐
小孔龙7 分钟前
GraphicBuffer 跨进程共享行业研究员34 分钟前
Android内置GPS与腾讯地图定位技术对比分析Android-Flutter1 小时前
android WMS 详解(二)游戏开发爱好者82 小时前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战天疆说2 小时前
04 稳定性与性能调优实录:OOM 崩溃、并发槽位、前缀缓存与 DSpark 调参csdn2015_2 小时前
springboot +mybatis 查询myql开启程序级别缓存隔窗听雨眠2 小时前
缓存增强生成CAG:用预加载KV缓存突破RAG实时检索瓶颈游戏开发爱好者83 小时前
iOS 推送怎么配置,APNs 推送证书、设备库与群发光影少年4 小时前
react离线缓存、图片缓存方案m0_3807438714 小时前
给大模型调用加一层本地缓存,让重复请求直接命中磁盘