使用getPreferece()设置第一次运行标志

时间:2011-05-10 11:47:36

标签: android preferences

我有这个代码应该在第一次运行时读取未设置的首选项:

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    SharedPreferences settings = getPreferences(MODE_PRIVATE);
    firstTime = settings.getBoolean("firstTime", true);
    Log.d("mything", "firstTime returns as: " + firstTime);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("firstTime", false);
    editor.commit();

变量“firstTime”始终返回false。 我正在卸载我的应用程序并重新加载它。

有人可以解释一下吗?

提前致谢

2 个答案:

答案 0 :(得分:2)

您使用的是Samsung Galaxy S和2.2.1固件吗?如果卸载了应用程序,则存在已删除共享首选项的错误。例如,请参阅评论here

答案 1 :(得分:0)

嗯奇怪。我运行你的代码并按预期工作。第一次运行它是日志

05-10 14:53:59.390: DEBUG/mything(4895): firstTime returns as: true

如果再次运行它,它总是记录

05-10 14:55:25.780: DEBUG/mything(4895): firstTime returns as: false

你确定你没有遗漏日志里的东西吗?

相关问题