【问题标题】:Android : SharedPreferences is not workingAndroid:SharedPreferences 不起作用
【发布时间】:2014-07-29 17:09:07
【问题描述】:

我正在尝试实现SharedPreferences。 我已经搜索了它,尝试了将近 12 个小时,但它仍然无法解决我的问题。

Java 代码

public static final String MyPREFERENCES = "banner_pref" ;
SharedPreferences SharedP;

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


    SharedP = getSharedPreferences("banner_pref", MainHelloballi.MODE_PRIVATE);
    SharedPreferences.Editor editor = SharedP.edit();
    editor.putBoolean("banner_pref", true);
    editor.commit();
    Boolean myValue = SharedP.getBoolean("banner_pref", true);

    if (myValue == true){
        bannerfull = (RelativeLayout) findViewById(R.id.banner_full);
        bannerfull.setVisibility(View.VISIBLE);

        editor = SharedP.edit();
        editor.putBoolean("banner_pref", false);
        editor.commit();

    }

    else {

        bannerfull = (RelativeLayout) findViewById(R.id.banner_full);
        bannerfull.setVisibility(View.GONE);


        //SharedP.edit().putBoolean("banner_pref", false).commit();

    }
}

我知道我的SharedPreferences 值是保持True。所以我的问题是,当我打开相同的Activity 时,如何让它不真实?

如果有人可以从我的代码中给我一个示例,我真的很感激。所以我可以从中学习。

之前非常感谢。

【问题讨论】:

  • 如果是isItFirstTime,你遇到了麻烦(总是如此),把它放在一个 if 语句中,首先检查它是否已经设置。然后设置为真,否则为假
  • 更改这一行: Boolean myValue = SharedP.getBoolean("banner_pref", true);到这个 Boolean myValue = SharedP.getBoolean("isItFirstTime", true);
  • @user1888162 抱歉,我说错了。我编辑代码
  • @Doomsknight 你能给我举个例子吗?不好意思问太多了

标签: java android sharedpreferences


【解决方案1】:

我刚刚对你的代码做了一点修正,它应该像这样工作

* Read the value with default is `true`, if the `SharedPreferences` doesn't exist before, certainly, it is the first time; `true` value is certain.
* if value is `true`, it is time to show the view; and then, update the value to `false` and save to `SharedPreferences`.
* else if value is `false`, hide the view.
* next time, when the `Activity` is launched, the value will be read as `false` (assuming there is none intervention in between, for example, users clear data from `Settings->Apps`...); the view is hidden, assuming there isn't any other action that update value to `true`.

代码如下:

public static final String MyPREFERENCES = "banner_pref" ;
SharedPreferences SharedP;

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

    // load sharedpref value 
    SharedP = getSharedPreferences("banner_pref", MainHelloballi.MODE_PRIVATE);
    Boolean myValue = SharedP.getBoolean("banner_pref", true); // default: true

    // load layout, we just need it anyway
    bannerfull = (RelativeLayout) findViewById(R.id.banner_full);

    // if true, show it
    if (myValue == true) {
        bannerfull.setVisibility(View.VISIBLE);
        // ok, now switch value to 'false' and commit to SharedP
        editor = SharedP.edit();
        editor.putBoolean("banner_pref", false);
        editor.commit();
    }
    // if false, hide it
    else {
        bannerfull.setVisibility(View.GONE);
    }
}

更新:每启动 3 次就显示一次。通过向显示的计数器数量添加一个新值。

public static final String MyPREFERENCES = "banner_pref" ;
SharedPreferences SharedP;

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

    // load sharedpref value 
    SharedP = getSharedPreferences("banner_pref", MainHelloballi.MODE_PRIVATE);
    Boolean myValue = SharedP.getBoolean("banner_pref", true); // default: true
    int counter = SharedP.getInt("counter", 1); // default starting from 1

    if(counter == 3) {
      myValue = true;
      // counter reset
      counter = 0;
    }

    // load layout, we just need it anyway
    bannerfull = (RelativeLayout) findViewById(R.id.banner_full);

    // if true, show it
    if (myValue == true) {
        bannerfull.setVisibility(View.VISIBLE);
        // ok, now switch value to 'false' and commit to SharedP
        editor = SharedP.edit();
        editor.putBoolean("banner_pref", false);
        editor.commit();
    }
    // if false, hide it
    else {
        bannerfull.setVisibility(View.GONE);
    }

    // each time loading, increase the counter
    ++counter;
    editor = SharedP.edit();
    editor.putInt("counter",  counter);
    editor.commit();
}

【讨论】:

  • 如果每次我打开应用程序,视图都会再次显示?
  • 嗯?只需删除与SharedPreferences 相关的所有部分;视图将一直显示。
  • 不,我的意思是如果用户重新启动应用程序,它会再次显示。但是,如果用户只是在应用程序中玩耍,它不会显示
  • 嗯?你在开玩笑……首先,it shows in app restart;第二,user playing around in application it won't show - 逻辑正确吗?
  • 等等,让我解释一下。因此,当用户第一次打开应用程序时,会显示 webview (its like a ads in fullscreen)。用户按下关闭按钮后,它不会再次显示,直到用户关闭应用程序并再次打开。
【解决方案2】:

我会抛出你的声明:

editor.putBoolean("banner_pref", true);

进入您的else 声明。如果您的if 被击中,这将防止它在更改之前被设置。

【讨论】:

  • 嗨,谢谢。但是当我第三次返回活动时,它会再次显示webview
  • 您可以做的解决方法是将SharedPreference 存储为另一个布尔值,以说明您的布尔值是否已设置,这样您就可以执行类似if(oldBooleanHasBeenSet){//do something} else{ //do something else} I以前做过,效果很好,只需在您的第一个 editor.commit() 之后将新布尔值设置为 true
猜你喜欢
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
相关资源
最近更新 更多