如何在android中停止CountDownTimer

Tah*_*aha 12 android countdowntimer android-studio

怎么停止这个计时器,任何想法?

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

 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();
Run Code Online (Sandbox Code Playgroud)

Ami*_*r_P 46

你应该把它定义为一个变量

 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();
Run Code Online (Sandbox Code Playgroud)

阅读更多:https://developer.android.com/reference/android/os/CountDownTimer.html

  • yourCountDownTimer.stop()?应该是 .cancel() 吗? (2认同)

Luc*_*rra 9

您可以在其中一个回调方法中调用this.cancel()或只是调用cancel()