在中断上保存countTimer()(Android)

时间:2017-02-27 00:43:18

标签: android android-studio timer oncreate

我正在创建一个游戏,我把一个计时器放到onCreate方法上,向用户显示时间,并在达到0时进行操作。我的方式正常但是因为我把它写入onCreate,时间继续倒计时,如果我旋转屏幕等它重置计时器秒。我知道为什么会发生这种情况,但是当应用程序暂停时我无法找到阻止它的方法,并且当应用程序继续时它会从它离开的地方继续。这是我的代码

 new CountDownTimer(15000, 1000) {

            public void onTick(long millisUntilFinished) {
                question.setText("seconds remaining: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                question.setText("done!");
                Intent i2 = new Intent(getApplicationContext(), CategoryActivity.class);
                startActivity(i2);
            }
        }.start();

我应该将milisUntilFinished保存到savedInstanceState并根据savedInstanceState启动计时器,还是有更简单的解决方案

编辑:所以我更新了我的代码以反映我问的问题。这样我可以在手机旋转时保存计时器的最后状态,但是当我打开菜单时暂停计时器仍然没有停止(暂停应用程序。)

 if(savedInstanceState!=null){
            seconds = savedInstanceState.getInt("seconds");
            new CountDownTimer(seconds * 1000, 1000) {

                public void onTick(long millisUntilFinished) {
                    seconds = (int) millisUntilFinished;
                    question.setText("seconds remaining: " + millisUntilFinished / 1000);
                }

                public void onFinish() {
                    question.setText("done!");
                    Intent i2 = new Intent(getApplicationContext(), CategoryActivity.class);
                    startActivity(i2);
                }
            }.start();
        }else {
            new CountDownTimer(15000, 1000) {

                public void onTick(long millisUntilFinished) {
                    seconds = (int) millisUntilFinished;
                    question.setText("seconds remaining: " + millisUntilFinished / 1000);
                }

                public void onFinish() {
                    question.setText("done!");
                    Intent i2 = new Intent(getApplicationContext(), CategoryActivity.class);
                    startActivity(i2);
                }
            }.start();
        }

2 个答案:

答案 0 :(得分:0)

我希望以下代码能为您提供帮助,

private long startTime = 0L;
private boolean threadStop = false;
private Handler myHandler = new Handler();
private Runnable updateTimerMethod = new Runnable() {
    public void run() {
        if (isAdded()) {
            timeInMillies = SystemClock.uptimeMillis() - startTime;
            //finalTime = timeSwap + timeInMillies;
            time.setText(miliSecondToMinSec(timeInMillies));

            if (!threadStop)
                myHandler.postDelayed(this, 1000);
        }
    }
};

@Override
public void onStart() {
    super.onStart();
    if (threadStop) {
        threadStop = false;
        startTime = SystemClock.uptimeMillis() - timeInMillies;
        myHandler.postDelayed(updateTimerMethod, 0);
    }
}

@Override
public void onStop() {
    threadStop = true;
    super.onStop();
}

答案 1 :(得分:0)

CounDwonTimer mCountTimer;
int seconds = 0;

然后取消mCountTimer中的onPause,然后从`onResme'开始

 protected void onResume(){
   super.onReusme();

   mCountTimer = new CountDownTimer(seconds*1000,1000){
       //......record the seconds
   }
}

protected void onPause(){
    if(mCountTimer != null)
       mCountTimer.cancel();
}

如果您的活动在旋转屏幕时 destory ,则需要onSaveInstanceState。否则您不需要此方法