在Android应用中存储高分

时间:2017-04-18 20:40:50

标签: java android android-intent sharedpreferences

我有一个应用程序,它在主屏幕上显示分数,但每次启动新游戏时都会设置为零。我想要它做的是存储该分数,只有在下一个分数较高时才会改变,因此它是最高分。

目前部署了一个hitCount方法,finish()方法用它来显示从游戏活动到主要活动的得分。

finish()方法如下:

public void finish(){

    Intent returnIntent = new Intent();
    returnIntent.putExtra("GAME_SCORE", gameView.getHitCount());
    setResult(RESULT_OK, returnIntent);
    super.finish();

}

在MainMenu类中,以下onActivityResult()方法用于获取要在主屏幕上显示的分数:

protected void onActivityResult(int requestCode, int resultCode, Intent retIntent) {
    // Check which request we're responding to
    if (requestCode == SCORE_REQUEST_CODE) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            if (retIntent.hasExtra("GAME_SCORE")) {
                int scoreFromGame = retIntent.getExtras().getInt("GAME_SCORE");
                tvScore.setText(Integer.toString(scoreFromGame));
                //highScore.setText(Integer.toString(scoreFromGame));
            }
        }   
    }

}

现在,我知道我必须使用像共享偏好这样的东西才能有机会存储分数,并在获得分数时用更高的数字替换它,但我无法弄清楚如何以及在何处。

一如既往,任何帮助都非常感激。

由于

2 个答案:

答案 0 :(得分:1)

将以下内容放入onCreate方法中以阅读高分。

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
// Read the high score from Shared Preference
int mHighScore = sharedPref.getInt(getString(R.string.high_score), 0);

使用共享首选项更新您的高分:

protected void onActivityResult(int requestCode, int resultCode, Intent retIntent) {
// Check which request we're responding to
    if (requestCode == SCORE_REQUEST_CODE) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            if (retIntent.hasExtra("GAME_SCORE")) {
                int scoreFromGame = retIntent.getExtras().getInt("GAME_SCORE");
                tvScore.setText(Integer.toString(scoreFromGame));
                //highScore.setText(Integer.toString(scoreFromGame));

                // Write the new high score to Shared Preference
                SharedPreferences.Editor editor = sharedPref.edit();
                if (scoreFromGame > mHighScore) {
                    editor.putInt(getString(R.string.high_score), scoreFromGame);
                    editor.commit();
                }
            }
        }   
    }
}

答案 1 :(得分:0)

您应该明确阅读有关SharedPreferences的内容并查看评论中给出的链接。如果您现在想要快速解决方案:

<强> MainActivity.java

SharedPreferences prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Set highscore text on start
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    int highscore = prefs.getInt(PreferenceHelper.KEY_HIGHSCORE, 0);
    tvHighScore.setText(Integer.toString(highscore));
}

protected void onActivityResult(int requestCode, int resultCode, Intent retIntent) {
    // Check which request we're responding to
    if (requestCode == SCORE_REQUEST_CODE) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            if (retIntent.hasExtra("GAME_SCORE")) {
                int scoreFromGame = retIntent.getExtras().getInt("GAME_SCORE");
                tvScore.setText(Integer.toString(scoreFromGame));

                int highscore = prefs.getInt(PreferenceHelper.KEY_HIGHSCORE, 0);  // Get highscore stored in prefs
                if (scoreFromGame > highscore) {
                    // Update text and save new highscore
                    tvHighScore.setText(Integer.toString(scoreFromGame));
                    prefs.edit().putInt(PreferenceHelper.KEY_HIGHSCORE, scoreFromGame).apply();
                }
            }
        }
    }

}

<强> PreferenceHelper.java

public final class PreferenceHelper {

    public static final String KEY_HIGHSCORE = "highscore";

}

您不必使用PreferenceHelper课程,但这是一个很好的做法。