如何在android中停止CountDownTimer

时间:2016-10-23 14:09:12

标签: android android-studio countdowntimer

如何停止此计时器,任何想法?

我想在每个查询中重置计时器,但它会继续。每个新查询都会添加新的计时器。怎么解决这个问题?

console.log('pre subscribe, number of observers is: ', _shareService.ShopItem$_.source.observers.length);

4 个答案:

答案 0 :(得分:41)

您应该将其定义为变量

 CountDownTimer yourCountDownTimer = new CountDownTimer(zaman, 1000) {                     //geriye sayma

        public void onTick(long millisUntilFinished) {

            NumberFormat f = new DecimalFormat("00");
            long hour = (millisUntilFinished / 3600000) % 24;
            long min = (millisUntilFinished / 60000) % 60;
            long sec = (millisUntilFinished / 1000) % 60;

            cMeter.setText(f.format(hour) + ":" + f.format(min) + ":" + f.format(sec));
        }

        public void onFinish() {
            cMeter.setText("00:00:00");
        }
    }.start();

yourCountDownTimer.cancel();

了解详情:https://developer.android.com/reference/android/os/CountDownTimer.html

答案 1 :(得分:7)

您可以在其中一个回调方法

中调用this.cancel()或简称cancel()

答案 2 :(得分:1)

static CountDownTimer countDownTimer = null;

    if (countDownTimer != null) {
        countDownTimer.cancel();
    }
    countDownTimer = new CountDownTimer(50000, 1000) {
        public void onTick(long millisUntilFinished) {
            Log.e("seconds remaining : ", "seconds remaining : " + millisUntilFinished / 1000);
        }

        public void onFinish() {
            Log.e("done!", "done!");
        }
    };
    countDownTimer.start();

答案 3 :(得分:0)

在kotlin中,您可以使用此命令

countDownTimer?.cancel()