如何在固定时间后暂停计时器

时间:2013-12-19 13:01:06

标签: android timer

在我的应用程序中,我有一个计时器。我希望计时器在60秒后自行暂停。任何人都可以帮助我......这就是我所做的事情

private Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
         mins = secs / 60;

        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        /*timerValue.setText("" + mins + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));*/

        timerValue.setText("" + mins + ":"
                + String.format("%02d", secs) );
        customHandler.postDelayed(this, 0);



    }

};

2 个答案:

答案 0 :(得分:1)

使用CountDownTimerwithPause类暂停和恢复计时器。参考这个

Pause CountDownTimer in Android when activity is not in front

覆盖它的onTick方法。当timmer达到60000呼叫暂停()时带有计时器对象。

答案 1 :(得分:0)

btn.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            startCountDownTimer();


        }
    });

protected void startCountDownTimer(){

         cdTimer = new CountDownTimer(total, 1000) {
                public void onTick(long millisUntilFinished) {
                    //update total with the remaining time left
                    total = millisUntilFinished;
                    tv.setText("seconds remaining: " +  millisUntilFinished/ 1000);
                }
                public void onFinish() {
                    tv.setText("done!");
                }
            }.start();
    }