1 回答

TA貢獻1829條經(jīng)驗 獲得超6個贊
您可以使用 SharedPreferences 來存儲屬于應(yīng)用程序的數(shù)據(jù)。
為 SharedPreferences 添加值
//Whenever you update the score call this function
void updateScore(int score){
SharedPreferences mySharedPref = getSharedPreferences("give Any Name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPref.edit();
editor.putInt("score", score);
editor.apply(); //this function will commit asynchronously
}
如果您需要隨時獲取分數(shù),請調(diào)用此函數(shù)。
//Whenever you update the score call this function
public int getScore(){
SharedPreferences mySharedPref = getSharedPreferences("give Any Name", Context.MODE_PRIVATE);
return mySharedPref.getInt("score", -1); //-1 is the default value that is returned if the value is not set using putInt
}
添加回答
舉報