你有两个选择,我会把选择留给你。
>共享首选项
这是一个Android独有的框架,允许您在键值框架中存储原始值(例如int,boolean和String,尽管严格来说String不是基元)。这意味着你给一个值一个名称,例如“homeScore”,并将值存储到此键。
SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("homeScore", YOUR_HOME_SCORE);
// Apply the edits!
editor.apply();
// Get from the SharedPreferences
SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
int homeScore = settings.getInt("homeScore", 0);
>内部存储
这在我看来,是你可能会寻找。你可以存储任何你想要的文件,所以这给你更多的灵活性。然而,这个过程可能更棘手,因为一切都将被存储为字节,这意味着你必须小心保持读写过程一起工作。
int homeScore;
byte[] homeScoreBytes;
homeScoreBytes[0] = (byte) homeScore;
homeScoreBytes[1] = (byte) (homeScore >> 8); //you can probably skip these two
homeScoreBytes[2] = (byte) (homeScore >> 16); //lines, because I've never seen a
//basketball score above 128, it's
//such a rare occurance.
FileOutputStream outputStream = getApplicationContext().openFileOutput(FILENAME, Context.MODE_PRIVATE);
outputStream.write(homeScoreBytes);
outputStream.close();
现在,您还可以查看External Storage,但我不建议在这种特殊情况下,因为外部存储可能不是以后。 (注意,如果你选择这个,它需要一个权限)