2
私は100(完全)から0(空)まで減少するプログレスバーを更新するカウントダウンタイマーを使用していますが、一時停止するとタイマーを一時停止したいアプリの再開時に再起動してください。Androidスタジオプログレスバーのカウントダウンタイマーの一時停止と再開
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
int progress = (int) (millisUntilFinished/100);
progressBar.setProgress(progress);
}
@Override
public void onFinish() {
progressBar.setProgress(0);
Intent intent = new Intent(getApplicationContext(), GameOver.class);
startActivity(intent);
finish();
}
}
そして、私の上で作成します:私はそうのようにカウントダウンタイマーを実装
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_screen);
progressBar = (ProgressBar)findViewById(R.id.timerBar);
progressBar.setProgress(100);
myCountDownTimer = new MyCountDownTimer(10000, 10);
myCountDownTimer.start();
}
これは、カウントダウンタイマーがまだバックグラウンドで実行され、0 – Zorqe
おおそのことについて考えていなかったに達したときに活動を終了しますが動作します。あなたができることは:一時停止するときにタイマー値を保存し、現在のタイマーをキャンセルし、最後に残りの時間で新しいタイマーを再始動させることです。残った時間を 'onTick(...)'メソッドに保存することができます。あなたのタイマーを作成するときは、 'countDownInterval'を低く設定して精度を上げてください。 – WannaGetHigh