Android(Kotlin)全局使用的SharedPreferences(SPUtil)

前言

下面是一个使用 Kotlin 编写的 Android SP(SharedPreferences)读写工具类,它使用全局 Context 来避免频繁创建 Context 实例。

工具类

自定义文件名

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
import android.content.Context
import android.content.SharedPreferences

/**
* SharedPreferences 工具类,使用全局Context
*/
object SPUtil {
// 全局SharedPreferences实例
private lateinit var sp: SharedPreferences

const val FILE_NAME: String = "share_data"

// 初始化方法,需要在Application中调用
fun init(context: Context) {
sp = context.getSharedPreferences(SPUtil.FILE_NAME, Context.MODE_PRIVATE);
}

/**
* 保存String类型数据
*/
fun putString(key: String, value: String) {
checkInit()
sp.edit().putString(key, value).apply()
}

/**
* 获取String类型数据
*/
fun getString(key: String, defaultValue: String = ""): String {
checkInit()
return sp.getString(key, defaultValue) ?: defaultValue
}

/**
* 保存Int类型数据
*/
fun putInt(key: String, value: Int) {
checkInit()
sp.edit().putInt(key, value).apply()
}

/**
* 获取Int类型数据
*/
fun getInt(key: String, defaultValue: Int = 0): Int {
checkInit()
return sp.getInt(key, defaultValue)
}

/**
* 保存Boolean类型数据
*/
fun putBoolean(key: String, value: Boolean) {
checkInit()
sp.edit().putBoolean(key, value).apply()
}

/**
* 获取Boolean类型数据
*/
fun getBoolean(key: String, defaultValue: Boolean = false): Boolean {
checkInit()
return sp.getBoolean(key, defaultValue)
}

/**
* 保存Float类型数据
*/
fun putFloat(key: String, value: Float) {
checkInit()
sp.edit().putFloat(key, value).apply()
}

/**
* 获取Float类型数据
*/
fun getFloat(key: String, defaultValue: Float = 0f): Float {
checkInit()
return sp.getFloat(key, defaultValue)
}

/**
* 保存Long类型数据
*/
fun putLong(key: String, value: Long) {
checkInit()
sp.edit().putLong(key, value).apply()
}

/**
* 获取Long类型数据
*/
fun getLong(key: String, defaultValue: Long = 0L): Long {
checkInit()
return sp.getLong(key, defaultValue)
}

/**
* 移除指定key的数据
*/
fun remove(key: String) {
checkInit()
sp.edit().remove(key).apply()
}

/**
* 清除所有数据
*/
fun clear() {
checkInit()
sp.edit().clear().apply()
}

/**
* 检查是否已初始化
*/
private fun checkInit() {
if (!::sp.isInitialized) {
throw IllegalStateException("SPUtil 尚未初始化,请先在Application中调用 init() 方法")
}
}
}

如果使用默认的可以

1
2
3
4
5
// 初始化方法,需要在Application中调用
fun init(context: Context) {
// 使用应用的全局Context
sp = PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
}

初始化

首先需要在 Application 类中初始化这个工具类:

1
2
3
4
5
6
7
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
// 初始化SPUtil,传入Application的context
SPUtil.init(this)
}
}

不要忘记在 AndroidManifest.xml 中配置你的 Application 类:

1
2
3
4
<application
android:name=".app.MyApplication"
<!-- 其他配置 -->
</application>

使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 保存数据
SPUtil.putString("username", "admin")
SPUtil.putInt("age", 25)
SPUtil.putBoolean("isLogin", true)

// 获取数据
val username = SPUtil.getString("username")
val age = SPUtil.getInt("age")
val isLogin = SPUtil.getBoolean("isLogin")

// 移除数据
SPUtil.remove("username")

// 清除所有数据
SPUtil.clear()

这个工具类的特点:

  • 使用单例模式,确保全局只有一个实例
  • 使用 Application 的全局 Context,避免内存泄漏
  • 提供了常用数据类型的读写方法
  • 包含初始化检查,防止未初始化就使用
  • 使用 apply() 方法提交修改,异步执行性能更好

如果需要使用自定义名称的 SharedPreferences 文件,可以修改 init 方法,使用 Context 的 getSharedPreferences() 方法代替 PreferenceManager.getDefaultSharedPreferences()。