class App : Application() {
companion object {
lateinit var instance: Application
private set
}
override fun onCreate() {
super.onCreate()
instance = this
}
}
二、SharedPreferences
class Preference<T>() {
private var keyName: String? = null
private var defaultValue: T? = null
constructor(keyName: String,defaultValue: T) : this() {
this.keyName = keyName
this.defaultValue = defaultValue
}
private val prefs: SharedPreferences by lazy {
App.instance.applicationContext.getSharedPreferences("default", Context.MODE_PRIVATE)
}
operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
Log.e("info", "调用$this 的getValue()")
return findSharedPreference(keyName!!, defaultValue!!)
}
op