Diese sollte gut funktionieren
package com.stackoverflow.example;
import android.os.CountDownTimer;
/**
* @author aminu on 5/7/2016.
*/
public class CountDownTimerHelper {
private int numOfRuns;
private long millisInFuture, countDownInterval;
private CountDownTimerHelper(int numOfRuns, long millisInFuture, long countDownInterval) {
//you may check for invalid arguments here like negative intervals
this.numOfRuns = numOfRuns;
this.millisInFuture = millisInFuture;
this.countDownInterval = countDownInterval;
}
private void tryRun() {
if (numOfRuns-- > 0) {
new CountDownTimerImpl(millisInFuture, countDownInterval).start();
}
}
public static void startCountDownTimers(int numOfRuns, long millisInFuture, long countDownInterval) {
new CountDownTimerHelper(numOfRuns, millisInFuture, countDownInterval).tryRun();
}
private class CountDownTimerImpl extends CountDownTimer {
/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
private CountDownTimerImpl(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
//mTextField.setText("seconds remaining: " + millisUntilFinished/1000);
}
@Override
public void onFinish() {
//notify user countdown ended by showing toast or something like that
//mTextField.setText("done " + "remaining " + numOfRuns + ");
tryRun();
}
}
}
dann, es zu benutzen nur CountDownTimerHelper.startCountDownTimers(numOfRuns,yourDuration,yourInterval)
Beispiel CountDownTimerHelper.startCountDownTimers(5,15000,1000) //this will run 5 successive countdowntimers with duration 15 seconds and interval 1 second
tun