How could I do that, for example, a method or condition runs a few minutes and then stops working when the time limit expires.
How could I do that, for example, a method or condition runs a few minutes and then stops working when the time limit expires.
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.
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();
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.