【问题标题】:can not run countdowntimer at all根本无法运行倒计时
【发布时间】:2018-06-26 18:41:39
【问题描述】:

我的倒数计时器有问题。我尝试了本网站上的一些解决方案和文章,但它们对我没有用。所以,请阅读我的代码...

我也用过

handler.postDelayed(new Runnable() { 

之前,这不是我的解决方案,但它工作正常。

主要问题是:

我想做如下的事情:

(button pressed)

do some codes1    
delay1

do other codes2    
delay2 

go back to *do some codes1* again.

简而言之,这是我的真实代码:

 itimesec--;
 setdelay();
 irepeat--;
 setrelax();

这是我的功能:

 public void setrelax(){
  CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {

        public void onTick(long millisUntilFinished1) {

            itotalsnozee--;
            TextToSpeechFunction(" "+itotalsnozee);
        }

        public void onFinish() {
            itotalsnozee=fitotalsnozee;
            isrelax=false;
            TextToSpeechFunction("do again");
        }
    }.start();

    yourCountDownTimer1.cancel();



}

我尝试使用 50000 的变量,但无论如何都没有用。

我尝试将 setrelax 功能代码直接放入 oncreate 中,但没有成功。 它刚刚跳到

}.start();

yourCountDownTimer1.cancel();

每次都出去。

我尝试了所有代码,没有任何延迟功能,它们运行正确。

请问我怎么了……

【问题讨论】:

    标签: android countdowntimer timedelay postdelayed


    【解决方案1】:

    您需要记住,使用CountDownTimer 时您的代码不会按顺序执行,因为它是异步工​​作的(通过 Handler)。

    让我们剖析您的代码。您的以下代码:

    public void setrelax(){
      // 1. Creating CountDownTimer
      CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
    
            public void onTick(long millisUntilFinished1) {
                // 2. onTick called
                ...
            }
    
            public void onFinish() {
              // 3. onFinish called
                ...
            }
        }.start();
    
       // 4. CountDownTimer is cancelled.
        yourCountDownTimer1.cancel();
    
    }
    

    将按以下顺序运行:

      1. 创建倒数计时器
      1. CountDownTimer 已取消。
      1. onTick 重复调用 49 次 (50000/1000 = 50 - 1 )。
      1. onFinish 调用

    所以,把你的算法改成这样:

    做一些代码1
    延迟1
    --> 延迟完成后,执行其他代码2。然后做延迟2

    你需要调用CountDownTimer.onFinish()中的下一个代码

    【讨论】:

    • 它非常实用。感激不尽
    【解决方案2】:

    我查看了您的代码,但没有发现任何大错误,但请尝试一下

    而不是

    .start();
    

    使用

    yourCountDownTimer1.start(); 
    

    并删除 yourCountTimer1.cancel(); 像这样:

     public void setrelax(){
      CountDownTimer yourCountDownTimer1 = new CountDownTimer(50000, 1000) {
    
            public void onTick(long millisUntilFinished1) {
    
                itotalsnozee--;
                TextToSpeechFunction(" "+itotalsnozee);
            }
    
            public void onFinish() {
                itotalsnozee=fitotalsnozee;
                isrelax=false;
                TextToSpeechFunction("do again");
            }
        };yourCountDownTimer1.start();
    
    
    
    }
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      以下是在我们的代码中运行 otp timer 的代码。您可以复制粘贴我提到的 cmets 请遵循相同的操作。

       CountDownTimer countDownTimer;  //define countDownTimer 
       countDownTimer.start();   // start countdown timer on some event like on click
      
        String x="Your code will expire In";
        String y="mins";
        counttimer(); call the method counttimer which includes all the code of timer
      
        public void counttimer(){
          countDownTimer = new CountDownTimer(30000, 1000) {
      
              public void onTick(long millisUntilFinished) {
                  String text = String.format(Locale.getDefault(), x+" %02d : %02d "+y,
                          TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60,
                          TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60);
                  phonever_timer.setText(text);   //set timer into textview object
              }
      
              public void onFinish() {
      
                  phonever_timer.setText("Otp Expired..!");
      
              }
          };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        相关资源
        最近更新 更多