Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simpler Android CountDownTimer
package foo;
import android.os.CountDownTimer;
public class SimpleCountDownTimer {
boolean repeat;
Runnable action;
CountDownTimer c;
public SimpleCountDownTimer(long interval, boolean repeat) {
this.c = new CountDownTimer(interval, interval) {
@Override
public void onTick(long millisUntilFinished) {}
@Override
public void onFinish() {
if (action != null)
action.run();
if (SimpleCountDownTimer.this.repeat)
this.start();
}
}.start();
}
public SimpleCountDownTimer(long interval, boolean repeat, Runnable action) {
this(interval, repeat);
this.setAction(action);
}
public void setAction(Runnable action) {
this.action = action;
}
public void cancel() {
this.c.cancel();
}
public void restart() {
c.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment