Created
July 30, 2021 02:16
-
-
Save AndreKR/6858305f0388e1b31f09b7172f184fa2 to your computer and use it in GitHub Desktop.
A simpler Android CountDownTimer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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