0

以下のような小さなゲームにプログレスバーを実装しました

    gameProgressBar =(ProgressBar)findViewById(R.id.GameProcess);

    thrd = new Thread(progressBarThread);
    thrd.start();

}

//Progress bar function
private Runnable progressBarThread = new Runnable(){

    public void run() {
        // TODO Auto-generated method stub
        while (GameProgressCount<60){  //60 = 1 minute
            try{
                myHandle.sendMessage(myHandle.obtainMessage());
                Thread.sleep(1000);

            }
            catch(Throwable t){
            }
        }
        thrd.stop();
    }

    Handler myHandle = new Handler(){

        @Override
        public void handleMessage(Message msg) {

                GameProgressCount++;
                gameProgressBar.setProgress(GameProgressCount);
        }
    };

私の質問は、デバイスの戻る/ホームボタンをクリックすると、アプリが最小化されます (または前の画面に移動します) が、進行状況バーのスレッドがバックグラウンドで実行されます。アプリ画面を最小化/開いたときに、スレッドを一時停止して再開することは可能ですか?

ありがとう

4

1 に答える 1

2

アクティビティの OnPause メソッドでスレッドを一時停止し、アクティビティの OnResume メソッドでスレッドを再開できます。これは非常に簡単です。ここで、GameProgressCount をそのまま保持し、元の場所から再開したい場合は、SharedPreference を使用して値を保存し、再開するときに最初に同じ値を使用することができます。

于 2011-11-03T03:41:59.780 に答える