繁体   English   中英

kotlin / java sharedPreferences在android studio中的含义

[英]kotlin / java sharedPreferences meaning in android studio

在 Kotlin / android studio 中,内置函数 sharedPreferences 到底是什么意思? 我已尝试阅读官方 android studio 文档中的文档,并希望简单了解如何使用它来共享数据。

Kotlin/Java 中的 SharedPreferences 可用于在系统内存中存储类型(String、Int、Long、Float、Boolean 等)的数据值,以便即使在应用程序终止后您也可以稍后检索它。

假设您要保存一个字符串值,

// Declare a shared preference
val sharedPref = getSharedPreferences('MySharedPref', MODE_PRIVATE)

// Initial value
val doorState: String = "Opened"

editDoorState(doorState)
// So whenever the doorState changes, we can save it, so even if the application is
// terminated or killed and restarts again, the last edited value can be retrieved.
// This is commonly used to store a single data value.



// Function to save the doorstate change(of type String) 
fun editDoorState(value: String) {
  val sharedPrefEditor = sharedPref.edit()

  // Where the 'doorState' is the SharedPreference Key.
  // To retrieve the value, key must be produced.
  sharedPrefEditor.putString('doorState', value)
  sharedPrefEditor.commit()
}

有关更多信息,请在此处查看 Android 的官方文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM