18034727885
2026-06-26 253f680fb9666751e0f9a8e5e8f2f9e3eb1894e3
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
package com.example.basecore.util
 
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import java.io.*
 
/**
 * author: dai
 * date : 2019-11-23
 * desc:  sp本地存储
 */
object SPUtils {
 
 
 
    private lateinit var sp: SharedPreferences
 
 
    fun initSp( app : Application) {
        sp = app.getSharedPreferences(
            "my_sp",
            Context.MODE_PRIVATE
        )
    }
 
    @SuppressLint("CommitPrefEdits")
    fun <T> putValue(name: String, value: T) = with(sp.edit()) {
        when (value) {
            is Long -> putLong(name, value)
            is String -> putString(name, value)
            is Int -> putInt(name, value)
            is Boolean -> putBoolean(name, value)
            is Float -> putFloat(name, value)
            else -> putString(name, serialize(value))
        }.apply()
    }
 
    @JvmStatic
    fun getString(key: String, default: String = "-1"): String {
        return sp.getString(key, default) ?: ""
    }
 
    @JvmStatic
    fun getBoolean(key: String, default: Boolean = false): Boolean {
        return sp.getBoolean(key, default)
    }
 
    @JvmStatic
    fun getInt(key: String, default: Int = -1): Int {
        return sp.getInt(key, default)
    }
 
    @JvmStatic
    fun getLong(key: String, default: Long = -1L): Long {
        return sp.getLong(key, default)
    }
 
 
    @JvmStatic
    fun getFloat(key: String, default: Float = -1f): Float {
        return sp.getFloat(key, default)
    }
 
    /**
     * 删除全部数据
     */
    fun clearSP() {
        sp.edit().clear().apply()
    }
 
    /**
     * 根据key删除存储数据
     */
    fun clearSPWithKey(key: String) {
        sp.edit().remove(key).apply()
    }
 
    /**
     * 序列化对象
     * @param person
     * *
     * @return
     * *
     * @throws IOException
     */
    @Throws(IOException::class)
    private fun <A> serialize(obj: A): String {
        val byteArrayOutputStream = ByteArrayOutputStream()
        val objectOutputStream = ObjectOutputStream(
            byteArrayOutputStream
        )
        objectOutputStream.writeObject(obj)
        var serStr = byteArrayOutputStream.toString("ISO-8859-1")
        serStr = java.net.URLEncoder.encode(serStr, "UTF-8")
        objectOutputStream.close()
        byteArrayOutputStream.close()
        return serStr
    }
 
    /**
     * 反序列化对象
     * @param str
     * *
     * @return
     * *
     * @throws IOException
     * *
     * @throws ClassNotFoundException
     */
    @Suppress("UNCHECKED_CAST")
    @Throws(IOException::class, ClassNotFoundException::class)
    private fun <A> deSerialization(str: String): A {
        val redStr = java.net.URLDecoder.decode(str, "UTF-8")
        val byteArrayInputStream = ByteArrayInputStream(
            redStr.toByteArray(charset("ISO-8859-1"))
        )
        val objectInputStream = ObjectInputStream(
            byteArrayInputStream
        )
        val obj = objectInputStream.readObject() as A
        objectInputStream.close()
        byteArrayInputStream.close()
        return obj
    }
 
}