一、获取方式
1、在activity中通过getPreferences(int mode)获取,作用域为当前activity
2、context.getSharedPreferences(String fileName, int mode),作用域为整个APP
二、模式介绍
1、Context.MODE_PRIVATE
默认模式,数据私有,作用域为当前APP,写入内容覆盖原有内容
2、Context.MODE_APPEND
数据私有,作用域为当前APP,写入内容追加原有内容后
3、Context.MODE_WORLD_READABLE
数据共享,其他应用也可以读取,Android 7.0废弃该模式,使用导致SecurityException
4、Context.MODE_WORLD_WRITEABLE
数据共享,其他应用也可以写入,Android 7.0废弃该模式,使用导致SecurityException
三、实例
package com.hualala.base.utils
import android.content.Context
import android.content.SharedPreferences
import com.hualala.base.common.BaseApplication
import java.io.*
/**
* SP工具类
*/
object AppPrefsUtils {
private var sp: SharedPreferences =
BaseApplication.context.getSharedPreferences(BaseApplication.context.packageName, Context.MODE_PRIVATE)
private var ed: SharedPreferences.Editor = sp.edit()
/**
* Boolean数据
*/
fun putBoolean(key: String, value: Boolean) {
ed.putBoolean(key, value)
ed.commit()
}
/**
* 获取boolean
*/
fun getBoolean(key: String, defValue: Boolean): Boolean {
return sp.getBoolean(key, defValue)
}
/**
* String数据
*/
fun putString(key: String, value: String) {
ed.putString(key, value)
ed.commit()
}
/**
* 获取字符串 (默认"")
*/
fun getString(key: String): String {
return getString(key, "")
}
/**
* 获取字符串
*/
fun getString(key: String, default: String): String {
return sp.getString(key, default) ?: default
}
/**
* Int数据
*/
fun putInt(key: String, value: Int) {
ed.putInt(key, value)
ed.commit()
}
/**
* 获取Int
*/
fun getInt(key: String, defValue: Int): Int {
return sp.getInt(key, defValue)
}
/**
* Long数据
*/
fun putLong(key: String, value: Long) {
ed.putLong(key, value)
ed.commit()
}
/**
* 获取Long
*/
fun getLong(key: String, defValue: Long): Long {
return sp.getLong(key, defValue)
}
/**
* Object数据
*/
fun <T> putObject(key: String, value: T) {
putString(key, serialize(value))
}
/**
* 获取Object
*/
fun <T> getObject(key: String, default: T): T {
return deSerialization(getString(key, serialize(default)))
}
/**
* Set数据
*/
fun putStringSet(key: String, set: Set<String>) {
val localSet = getStringSet(key).toMutableSet()
localSet.addAll(set)
ed.putStringSet(key, localSet)
ed.commit()
}
/**
* 默认空set
*/
fun getStringSet(key: String): Set<String> {
val set = setOf<String>()
return sp.getStringSet(key, set) ?: set
}
/**
* 删除key数据
*/
fun remove(key: String) {
ed.remove(key)
ed.commit()
}
/**
* 序列化对象
*/
@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
}
/**
* 反序列化对象
*/
@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
}
}