第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Android 應(yīng)用程序中保存不斷變化的分數(shù)

在 Android 應(yīng)用程序中保存不斷變化的分數(shù)

瀟瀟雨雨 2022-12-15 10:51:59
我正在用 java 創(chuàng)建一個 android 應(yīng)用程序,游戲中的分數(shù)一直在變化,每秒多次。我想創(chuàng)建一個保存文件以便在用戶返回應(yīng)用程序時能夠跟蹤分數(shù),問題是我應(yīng)該如何保存它?每當分數(shù)發(fā)生變化時,每秒多次寫入保存文件似乎效率極低。我考慮過將它保存在 onDestroy() 方法中,但我聽說不能保證它會被調(diào)用......那么,在不必每秒多次訪問文件的情況下,保存樂譜的保證方法是什么?
查看完整描述

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


}


查看完整回答
反對 回復 2022-12-15
  • 1 回答
  • 0 關(guān)注
  • 75 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號