8
votes

Comment créer une boucle infinie

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


0 commentaires

8 Réponses :


1
votes

Vous pouvez simplement utiliser une boucle tandis que:
pendant (vrai) {
// fait des choses
}
Quand il a fait "The Stuff", il va recommencer, infini!


2 commentaires

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



16
votes

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


0 commentaires

5
votes

est de redémarrer votre minuterie quand son a terminé :) comme ceci: xxx


0 commentaires

3
votes

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: xxx


0 commentaires

1
votes

Pour maintenir votre minuterie de travail, il suffit de mettre xxx

dans le bloc bloquer


1 commentaires

cela.start () ;;;



8
votes

pour le compte enregistré NombreDowner ( long millisinfuture, long comptagedownerval) xxx

où long.max_value = 9223372036854775807 Milisecondes ou environ 292 millions des années (secondes de plus ou moins)

Ce n'est pas infini mais incroyablement long.


0 commentaires

4
votes

moyen simple de créer une boucle infinie:

chaque seconde méthode d'appel xxx


0 commentaires

0
votes

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


0 commentaires