実際には非常に単純です。SharedPreferences を使用してキーと値のペアを保存します。あなたはどちらかを呼び出すことができます
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
GameScreen から定数を定義します。
public static final String KEY_SCORE = "score";
保存の鍵として使用します。
実際に保存するには、エディターが必要です。
SharedPreferences.Editor mEditor = mPrefs.edit();
//save data now, mPlayerScore is the score you keep track of
//if it's another type call putString(), etc
mEditor.putInt(KEY_SCORE, mPlayerScore);
//if that is the only thing you want for noe
//close and commit
mEditor.commit();
保存されたスコアを取得するには、 onCreate() 中に次のことができます。
public void getUserProgress() {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
//no need for an editor when retrieving
mPlayerScore = mPrefs.getInt(KEY_SCORE, 0);
//second value passed was the default score
//if no score was found
}
新しいキャラクターのロックが解除されていることを確認するには、ゲームごとに次のコードを呼び出すことができます。
private void checkForUserUnlocks() {
if (mPlayerScore >= MyUnlockableCharacter.SCORE_NEEDED_TO_UNLOCK)
MyUnlockableCharacter.isUnlocked = true;
//and same for other unlockabld characters
}
SharedPreferences にアクセスする方法は他にもあります。詳細が必要な場合はお知らせください。