TimerTask, Help (modified)

1

Start the method when starting the program, what I need is for the loop to run and then generate a timeout (1 minute) and rerun the task past the timeout (but also run the loop. . and so on) As I have it now, when you enter the method, it launches the waiting time.

   public void comprobarFallos() {
timerTask = new TimerTask() {
    @Override
    public void run() {
        for (int i = 0; i < listaFallos.size(); i++) {
            if (listaFallos.get(i).equals("1")) {
                playAudios.playAudio(listaAudios.get(i));
            }
        }
    }
};
// Aquí se pone en marcha el timer CADA 2 MINUTOs
timer.scheduleAtFixedRate(timerTask, 0, TIEMPOESPERA);}
    
asked by R.Priego 18.11.2016 в 17:44
source

2 answers

0

The piece of code you put in is correct as far as I can see it. when executing the comprobarFallos() method, the timer is set to 2 minutes to execute the timerTask, and then the method returns. Synchronously, the timer runs behind until the timer signal occurs and executes the run

method

I think your question is missing specification as to what you want to happen, and context is missing so we know where the error might be. If you say that when running the program the timer is setting automatically without calling comprobarFallos() . Then there is probably an error in another part of the code, you are possibly calling the method elsewhere. As I can not see the rest of the code I would not know for sure; As far as I can see, it's correct.

Please explain more what you mean by doing the check (loop), what loop do you mean?

Hopefully my answer will help. Anyway, if you add more information to your question, I will edit my answer to try to help you more; but with the little information you gave, it's hard to do more.

    
answered by 18.11.2016 в 18:47
0

At the end of the loop, declare your TimerTask , start the first time immediately and then define it in TIEMPOESPERA :

 for (int i = 0; i < listaFallos.size(); i++) {
                if (listaFallos.get(i).equals("1")) {
                    playAudios.playAudio(listaAudios.get(i));
                }
  }

  //al finalizar el bucle creas TimerTask

     timerTask = new TimerTask() {
        @Override
        public void run() {
         //Realiza tarea!...
        }
    };
    // Aquí se pone en marcha el timer CADA 2 MINUTOs
    timer.schedule(timerTask, 0, TIEMPOESPERA);        
    
answered by 18.11.2016 в 19:27