gpt4 book ai didi

java - 如何设置共享首选项

转载 作者:行者123 更新时间:2023-12-01 08:07:14 25 4
gpt4 key购买 nike

我正在尝试设置共享首选项,但以下代码在两种情况下都会导致 false

当标志不存在时,我首先获取该标志的值,并期望返回一个 false。但是,然后我将值设置为 true 并再次获取标志,这次我期望 true 但它仍然是 false

    SharedPreferences sharedPref = getSharedPreferences("myapp",0);
//fetch value when it does not exist
Boolean mobileFlag = sharedPref.getBoolean("mobile_flag", false);
Log.d("mobileFlag1", mobileFlag+"");
//set the value
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("mobile_flag",true);
//fetch value when it has been set.
mobileFlag = sharedPref.getBoolean("mobile_flag", false);
Log.d("mobileFlag2", mobileFlag+"");

两次日志消息的结果都是:

D/mobileFlag1﹕ false
D/mobileFlag2﹕ false

最佳答案

您没有提交新值

editor.putBoolean("mobile_flag",true).commit();

关于java - 如何设置共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20706892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com