【问题标题】:How to stop CountDownTimer in android如何在android中停止CountDownTimer
【发布时间】:2016-10-23 14:09:12
【问题描述】:

如何停止这个计时器,知道吗?

我想在每个查询中重置计时器,但它会继续。每个新查询都会添加新计时器。如何解决?

 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();

【问题讨论】:

    标签: android android-studio countdowntimer


    【解决方案1】:

    你可以把它赋值给一个变量,然后在变量上调用cancel()

    CountDownTimer yourCountDownTimer = new CountDownTimer(zaman, 1000) {                    
        public void onTick(long millisUntilFinished) {}
    
        public void onFinish() {}
    
        }.start();
    
    yourCountDownTimer.cancel();
    

    或者你可以在你的计数器范围内调用cancel()

    new CountDownTimer(zaman, 1000) {                    
        public void onTick(long millisUntilFinished) {
            cancel();
        }
    
        public void onFinish() {}
    
        }.start();
    

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

    【讨论】:

    • 非常感谢 yourCountDownTimer.cancel();作品
    • yourCountDownTimer.stop()?应该是 .cancel() 吗?
    • 是的,你是对的,我是在无法访问 android studio 时写的:D @KenRatanachaiS。
    【解决方案2】:

    您可以在其回调方法之一中调用this.cancel() 或简单地调用cancel()

    【讨论】:

      【解决方案3】:

      静态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();
      

      【讨论】:

        【解决方案4】:

        在 kotlin 中你可以使用这个命令

        countDownTimer?.cancel()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多