共享首选项不起作用,android,kotlin?
Shared Preferences is not working, android, kotlin?
我正在尝试将 uuid 保存在应用程序内存中。为此,我执行使用共享首选项并使用来自 developer.android.com 的指南
从这个地方正好 PLACE
private fun setPreferences(response: JSONObject) {
val sharedPref = this.getPreferences(Context.MODE_PRIVATE) ?: return
with(sharedPref.edit()) {
putString(
getString(R.string.PREFERENCE_FILE_KEY),
response.get("uuid").toString()
)
apply()
}
这是我的 strings.xml 文件
<resources>
<string name="app_name">Book of recipes</string>
<string name="prompt_login">Login</string>
<string name="prompt_password">Password</string>
<string name="prompt_confirm_password">Confirm password</string>
<string name="action_sign_in_short">Sign in</string>
<string name="have_an_account">Already have an account?</string>
<string name="dont_have_an_account">Don\'t have an account?</string>
<string name="rabbits_with_carrots">Rabbits with carrots</string>
<string name="login_error">Login must be 4–12 characters long</string>
<string name="password_error">Password must be 4–16 characters long</string>
<string name="password_error_reconciliation">Wrong password re-entered</string>
<string name="characters_check_error">Field contains invalid characters</string>
<string name="PREFERENCE_FILE_KEY"></string>
</resources>
在尝试理解为什么这不起作用之后,我在 setPreference fun 中编写了这行代码
println("UUID : " + response.get("uuid").toString())
println("Preference file key : " + getSharedPreferences(
getString(R.string.PREFERENCE_FILE_KEY), Context.MODE_PRIVATE))
println("R string : " + applicationContext.getString(R.string.PREFERENCE_FILE_KEY))
并以此作为回应:
I/System.out: UUID : 2c912ffb-01c0-430f-91ca-4ebe7d663225
I/System.out: Preference file key : android.app.SharedPreferencesImpl@638299c
R string :
那么问题来了,为什么我在尝试获取共享首选项时会得到这个结果,以及如何将这个 uuid 放入 strings.xml 文件并取回它?
错误是您没有为您的偏好设置键,将<string name="PREFERENCE_FILE_KEY"></string>
更改为<string name="PREFERENCE_FILE_KEY">whatever</string>
。 whatever
(或任何其他词)是您正在保存的变量的标识符,称为键。
我认为您误解了使用 SharedPreferences 可以实现什么,您不能,而且在运行时更改 strings.xml 毫无意义,因为它在编译时只读。使用 SharedPreferences,您可以将变量保存在内部存储中并稍后检索它们,即使您的应用在此期间已停止 运行。
保存示例:
fun Context.setSharedPreference(prefsName: String, key: String, value: String) {
getSharedPreferences(prefsName, Context.MODE_PRIVATE)
.edit().apply { putString(key, value); apply() }
}
加载示例:
fun Context.getSharedPreference(prefsName: String, key: String): String {
getSharedPreferences(prefsName, Context.MODE_PRIVATE)
?.getString(key, "Value is empty!")?.let { return it }
return "Preference doesn't exist."
}
您错过了 <string name="PREFERENCE_FILE_KEY">UDID_KEY</string>
。将您的字符串文件从 <string name="PREFERENCE_FILE_KEY"></string>
更新为 <string name="PREFERENCE_FILE_KEY">UDID_KEY</string>
,因为共享首选项使用唯一键来保存和检索相应的值。
'UDID_KEY' 这个字符串键可以是任何你想要的。但是为了取回 UDID,您需要使用用于保存数据的相同密钥,例如 UDID_KEY 这里。
我正在尝试将 uuid 保存在应用程序内存中。为此,我执行使用共享首选项并使用来自 developer.android.com 的指南 从这个地方正好 PLACE
private fun setPreferences(response: JSONObject) {
val sharedPref = this.getPreferences(Context.MODE_PRIVATE) ?: return
with(sharedPref.edit()) {
putString(
getString(R.string.PREFERENCE_FILE_KEY),
response.get("uuid").toString()
)
apply()
}
这是我的 strings.xml 文件
<resources>
<string name="app_name">Book of recipes</string>
<string name="prompt_login">Login</string>
<string name="prompt_password">Password</string>
<string name="prompt_confirm_password">Confirm password</string>
<string name="action_sign_in_short">Sign in</string>
<string name="have_an_account">Already have an account?</string>
<string name="dont_have_an_account">Don\'t have an account?</string>
<string name="rabbits_with_carrots">Rabbits with carrots</string>
<string name="login_error">Login must be 4–12 characters long</string>
<string name="password_error">Password must be 4–16 characters long</string>
<string name="password_error_reconciliation">Wrong password re-entered</string>
<string name="characters_check_error">Field contains invalid characters</string>
<string name="PREFERENCE_FILE_KEY"></string>
</resources>
在尝试理解为什么这不起作用之后,我在 setPreference fun 中编写了这行代码
println("UUID : " + response.get("uuid").toString())
println("Preference file key : " + getSharedPreferences(
getString(R.string.PREFERENCE_FILE_KEY), Context.MODE_PRIVATE))
println("R string : " + applicationContext.getString(R.string.PREFERENCE_FILE_KEY))
并以此作为回应:
I/System.out: UUID : 2c912ffb-01c0-430f-91ca-4ebe7d663225
I/System.out: Preference file key : android.app.SharedPreferencesImpl@638299c
R string :
那么问题来了,为什么我在尝试获取共享首选项时会得到这个结果,以及如何将这个 uuid 放入 strings.xml 文件并取回它?
错误是您没有为您的偏好设置键,将<string name="PREFERENCE_FILE_KEY"></string>
更改为<string name="PREFERENCE_FILE_KEY">whatever</string>
。 whatever
(或任何其他词)是您正在保存的变量的标识符,称为键。
我认为您误解了使用 SharedPreferences 可以实现什么,您不能,而且在运行时更改 strings.xml 毫无意义,因为它在编译时只读。使用 SharedPreferences,您可以将变量保存在内部存储中并稍后检索它们,即使您的应用在此期间已停止 运行。
保存示例:
fun Context.setSharedPreference(prefsName: String, key: String, value: String) {
getSharedPreferences(prefsName, Context.MODE_PRIVATE)
.edit().apply { putString(key, value); apply() }
}
加载示例:
fun Context.getSharedPreference(prefsName: String, key: String): String {
getSharedPreferences(prefsName, Context.MODE_PRIVATE)
?.getString(key, "Value is empty!")?.let { return it }
return "Preference doesn't exist."
}
您错过了 <string name="PREFERENCE_FILE_KEY">UDID_KEY</string>
。将您的字符串文件从 <string name="PREFERENCE_FILE_KEY"></string>
更新为 <string name="PREFERENCE_FILE_KEY">UDID_KEY</string>
,因为共享首选项使用唯一键来保存和检索相应的值。
'UDID_KEY' 这个字符串键可以是任何你想要的。但是为了取回 UDID,您需要使用用于保存数据的相同密钥,例如 UDID_KEY 这里。