OK, j'ai besoin de créer une boucle infinie sur un compte à rebours. Mon code est le suivant:
public void countdown() { if (x != null) { x.cancel(); } x = new CountDownTimer(20000, 1000) { public void onTick(long millisUntilFinished) { } public void onFinish() { showNotification(); } }; x.start(); }
8 Réponses :
Vous pouvez simplement utiliser une boucle tandis que:
pendant (vrai) {
// fait des choses
} code>
Quand il a fait "The Stuff", il va recommencer, infini! P>
Cela ne redémarrera pas le compte à rebours infiniment, mais il instaurera et démarrera un butin de minuteries infiniment, ce qui n'est pas ce que le gars veut :)
La CPU utilise le lot avec cette méthode et besoin d'utiliser asynccaptask
J'espère que cela vous aidera.
public void countdown(){ if (x != null) { x.cancel(); } x = new CountDownTimer(20000, 1000) { public void onTick(long millisUntilFinished) { } public void onFinish() { showNotification(); x.start(); } }; }
est de redémarrer votre minuterie quand son a terminé :)
comme ceci:
Pourquoi pas simplement utiliser une minuterie régulière? Il va répéter sur un intervalle spécifié jusqu'à ce que vous appelle Annuler (), quelque chose comme:
Pour maintenir votre minuterie de travail, il suffit de mettre dans le bloc code> bloquer p> p> p> p> P>
cela.start () ;;;
pour le compte enregistré NombreDowner ( long fort> millisinfuture, long fort> comptagedownerval) où long.max_value = 9223372036854775807 Milisecondes ou environ 292 millions des années (secondes de plus ou moins) p> Ce n'est pas infini mais incroyablement long. p> p>
chaque seconde méthode d'appel forte> p>
Eh bien, j'ai mis en place une minuterie indéfinie qui prend plusieurs auditeurs et les appelle simultanément sur un intervalle spécifique.
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(); } }