如何创建无限循环

Mil*_*Way 5 android loops timer countdowntimer

好的,我需要在倒计时上创建一个无限循环.我的代码是:

public void countdown() {
    if (x != null) {
        x.cancel();
    }

    x = new CountDownTimer(20000, 1000) {
        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
            showNotification();
        }
    };
    x.start();
}
Run Code Online (Sandbox Code Playgroud)

x只是一个静态倒计时器变量.问题是我尝试了很多方法来使上面的代码工作,我的意思是当倒计时结束时,它显示该通知,它应该重新开始等等......但我找不到办法做到这一点.

fre*_*oid 12

希望这会帮助你.

public void countdown(){
    if (x != null) {
        x.cancel();
    }
    x = new CountDownTimer(20000, 1000) {
       public void onTick(long millisUntilFinished) {
        }
       public void onFinish() {
           showNotification();
            x.start();
        }
    };
 }
Run Code Online (Sandbox Code Playgroud)


mag*_*nes 8

用于记录 CountDownTimer( long millisInFuture, long countDownInterval)

  // A not so infinite but close to infinte interval for each second
  CountDownTimer cdt=new CountDownTimer(Long.MAX_VALUE, 1000) { .... }
Run Code Online (Sandbox Code Playgroud)

其中 Long.MAX_VALUE = 9223372036854775807 毫秒或大约 2.92 亿年(秒或多或少)

它不是无限的,而是令人难以置信的长。


Hou*_*ine 5

是他完成后重启你的计时器:)像这样:

   x = new CountDownTimer(20000, 1000) {
            public void onTick(long millisUntilFinished) {
            }

            public void onFinish() {
                showNotification();
                start();// here, when your CountDownTimer has finished , we start it again :)
            }
        };
        x.start();
Run Code Online (Sandbox Code Playgroud)


Bro*_*yte 1

您可以只使用 while 循环: 当它完成“事情”时,它将再次开始,无限!
while (true) {
// do stuff
}