-->

不能运行countdowntimer在所有(can not run countdowntimer a

2019-10-29 07:48发布

我有一个倒计时问题timer.I尝试在这个网站的一些解决方案和文章,但他们从来没有为我工作。 所以,请阅读我的代码...

我也用

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



}

我试图用一个变量insted的50000但无论如何,它是没有用的。

我试图把setrelax funtion码直接进入OnCreate中,但毫无效果。 它只是跃升至

}.start();

yourCountDownTimer1.cancel();

每一个时代,走出去。

我尝试了所有的代码没有任何延迟功能,他们拼命地跑正确。

什么是我错了,请...

Answer 1:

你要记住,你的代码不会被连续使用时,执行CountDownTimer ,因为它工作在异步模式(通过处理程序)。

让我们来剖析你的代码。 你下面的代码在这里:

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. 创建CountDownTimer
    1. CountDownTimer被取消。
    1. onTick称为49倍重复(1000分之50000= 50 - 1)。
    1. onFinish叫

所以,你的算法改变这样的:

做一些codes1
DELAY1
- >当延迟完成,做其他的代码2。 然后做延迟2

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



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



}

希望能帮助到你。



Answer 3:

下面是我们的code.you运行OTP定时器可以复制粘贴我所提到的意见,请遵循相同的代码。

 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..!");

        }
    };


文章来源: can not run countdowntimer at all