How can I execute a method for about 10 minutes on Android and after that time stop running?

3

How could I do that, for example, a method or condition runs a few minutes and then stops working when the time limit expires.

    
asked by JDeveloper 08.07.2016 в 01:24
source

3 answers

1

If you want a method to run several times for 10 minutes, you can do it as Elenasys says, but if you want to run a method, maybe it has a cyclo, then you may need to use Thread .

public class miThread extends Thread {
    @Override
    public run() {
       //Tu método
    }
}

And out of that class, what Elenasys did, but trying to stop the Thread.

try{
   mithread.stop();
}catch(InterruptedException e) {}

So the try runs after 10 minutes.

    
answered by 25.07.2016 в 19:22
0

This is a way to create a countdown in your case from 10 to 0:

new CountDownTimer(20000, 1000) {

 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("done!");
 }
  }.start();
    
answered by 25.07.2016 в 22:04
-2

This is a way to achieve what you want:

long inicio = System.currentTimeMillis();
long terminacion = inicio + 600*1000; // 600 segundos * 1000 millisegundos
while (System.currentTimeMillis() < terminacion)
{
    // ejecuta metodo.
}
//termina método después de 10 minutos.
    
answered by 08.07.2016 в 05:30