Skip to content

Instantly share code, notes, and snippets.

@AndreKR
Created July 30, 2021 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreKR/6858305f0388e1b31f09b7172f184fa2 to your computer and use it in GitHub Desktop.
Save AndreKR/6858305f0388e1b31f09b7172f184fa2 to your computer and use it in GitHub Desktop.
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