关闭应用程序时删除分数

时间:2016-03-18 16:59:22

标签: android-activity time save local-storage

当我在应用程序中时,我的最佳成绩很好,并且我保存了最好的分数。如果我关闭app最后保存的值不被识别并为我设置新的最佳分数。

 private void Update(){

     if(number > highscore2) {
        highscore2 = number;
                 }
    }

    // set highscore sharedpreferences
private void HighScore() {
    Update();
    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
     editor.putInt("key", number);
    editor.putInt("key1", highscore2);
    editor.putInt("key2", highscore3);
    editor.commit();

    Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_LONG).show();

    Intent it = new Intent(getApplicationContext(), HighScores.class);

}

1 个答案:

答案 0 :(得分:0)

  

我解决了我的问题:

 Update();
                    if (number > prefs.getInt("key", 0)) {
                        editor.putInt("key", number);
                        editor.putString("tag", value);

                    } else if (number < prefs.getInt("key", 0)) {
                        if (number > prefs.getInt("key1", 0)) {
                            editor.putInt("key1", number);
                            editor.putString("tag1", value1);
                        }
                    }
                    if (number < prefs.getInt("key1", 0)) {
                        editor.putInt("key2", number);
                        editor.putString("tag2", value2);
                    }
  

更新()是:

 private void Update() {
    if (number > highscore2) {
        highscore2 = number;
    }
}