1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| 支持对象存储(自动 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 省略 }
|