如何创建一个无限循环

9

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

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

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

        public void onFinish() {
            showNotification();
        }
    };
    x.start();
}

x是一个静态的倒计时变量。问题在于,我尝试了许多方法来使上述代码工作,我的意思是当倒计时结束并显示通知时,它应该重新开始,如此循环......但我找不到一种方法来实现它。

8个回答

17

希望这能对你有所帮助。

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

12

记录下来 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) { .... }

其中 Long.MAX_VALUE = 9223372036854775807 毫秒,大约是 292 百万年(多或少几秒)

它不是无限长,但非常长。


6
重新启动您的计时器,当他的时间结束时 :) 就像这样:

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

4

创建无限循环的简单方法:

每一秒调用一次方法

new CountDownTimer(1000, 1000) 
   {
        public void onTick(long l) {}
        public void onFinish() 
        {
          //Code hear
          start();
        }
    }.start();

3
为什么不能使用普通的计时器?它会在指定的间隔重复,直到您调用cancel()方法停止,类似于:
public void countdown() { 
    if (x != null) {
        x.cancel();
    }

    x = new Timer("timerName");
    x.schedule(_timerTask, 0, 20000);
}

private static final TimerTask _timerTask = new TimerTask() {
    @Override
    public void run() {
        showNotification();
    }
};

1
你可以使用 while 循环:
while (true) {
// 做一些事情
}

当它完成了“这些事情”后,它会重新开始,无限循环!


这不会无限重启CountDownTimer,但它将实例化和启动无数个计时器,这不是那个人想要的 :) - Houcine
使用这种方法会导致CPU使用率很高,需要使用异步任务。 - Ashish Sahu

1
保持您的计时器运行,只需放置:
<countdowntime>.start(); 

onfinish代码块中

this.start();;; - Isaak Osipovich Dunayevsky
或者只需 start(); - Sambhav Khandelwal

0

嗯,我已经实现了一个不确定的计时器,它可以接受多个监听器,并在特定间隔同时调用它们。

import android.os.CountDownTimer;
import java.util.Arrays;

public class InfiniteCounter extends CountDownTimer {

    private static final int MAX_LISTENERS = 100;

    private static InfiniteCounter timer;
    private static InfiniteCounterListener[] listenerList = new InfiniteCounterListener[MAX_LISTENERS];

    private InfiniteCounter(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    //Milliseconds Intervals in which the counter should call its listeners 
    public static InfiniteCounter initInstance(int intervalMillis) {
        removeAllListeners();
        if (timer == null) {
            timer = new InfiniteCounter(60 * 60 * 1000, intervalMillis);
            timer.start();
        }
        return timer;
    }

    public static void attachListener(InfiniteCounterListener listener) {
        for (int i = 0; i < MAX_LISTENERS; i++) {
            if (listenerList[i] == null) {
                listenerList[i] = listener;
                break;
            }
        }
    }

    public static void removeListener(InfiniteCounterListener listener) {
        for (int i = 0; i < MAX_LISTENERS; i++) {
            if (listenerList[i] == listener) {
                listenerList[i] = null;
                break;
            }
        }
    }

    private static void removeAllListeners() {
        Arrays.fill(listenerList, null);
    }

    public static void stopTimer() {
        removeAllListeners();
        if (timer != null) timer.cancel();
        timer = null;
    }

    @Override
    public void onTick(long l) {
        for (int i = 0; i < MAX_LISTENERS; i++) {
            if (listenerList[i] != null) listenerList[i].onTick();
        }
    }

    @Override
    public void onFinish() {
        timer.start();
    }

    public interface InfiniteCounterListener {
        void onTick();
    }
}

只需将监听器附加到此类,并使用单个计时器,它就可以调用多个监听器 - 这使其非常优化性能。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接