I would like to start repeating two lines of code every 5 seconds when I press the button START and end it, when I press the button STOP. I was trynig with a TimerTask and H
You can use CountDownTimer
as the following method:
private CountDownTimer timer;
timer = new CountDownTimer(5000, 20) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
try{
yourMethod();
}catch(Exception e){
Log.e("Error", "Error: " + e.toString());
}
}
}.start();
And then to call the timer again:
public void yourMethod(){
//do what you want
timer.start();
}
To cancel the timer, you can call timer.cancel();
Hope it helps!