应用关闭后保存int / boolean值

时间:2013-08-21 15:40:19

标签: android

如何在关闭应用程序后保存int / boolean值,这样当我启动应用程序时,int值将转到textview,就像游戏中的个人记录一样?

int personalRecord = 30;

3 个答案:

答案 0 :(得分:2)

您可以将值保存到SharedPreferences:

SharedPreferences mPreferences = getSharedPreferences("CurrentUser",
            MODE_PRIVATE);

保存:

SharedPreferences.Editor editor = mPreferences.edit(); 
editor.putInt("personalRecord", 30);
editor.commit();

要检索:

mPreferences.getInt("personalRecord", defaultValue);

答案 1 :(得分:0)

答案 2 :(得分:0)

使用SharedPreferences
请尝试使用此代码。

        int count = 2;
        boolean bool = true;
        SharedPreferences preferences = getSharedPreferences("key", 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("int", count);
        editor.putBoolean("bool", bool);
        editor.commit();


SharedPreferences

中检索价值
        SharedPreferences preferences = getSharedPreferences("key", 0);
        int count = preferences.getInt("int",0);
        boolean bool = preferences.getBoolean("bool",false);