public abstract class IntervalTimer extends Object
Example code:
new IntervalTimer(60000, 3000) {
@Override
public void onInterval(long remain) {
// This method will be called at the 3000th millisecond and repeatedly called every 3000 milliseconds.
}
@Override
public void onFinish() {
// This method will be called at the 60000th millisecond.
}
}.schedule();
Note: IntervalTimer#onInterval(long) is synchronized.
If the execution duration of this method is longer than the interval, the next call waits until the previous callback
is complete. For example, if the interval is 3000 milliseconds, and the IntervalTimer#onInterval(long)
callback lasts 4000 milliseconds, the first call is at the 3000th millisecond, and the next call is at the 9000th
millisecond.
| 构造器 | 说明 |
|---|---|
IntervalTimer(long duration,
long interval) |
Creates an
IntervalTimer with a given work duration and interval. |
public IntervalTimer(long duration,
long interval)
IntervalTimer with a given work duration and interval.duration - Indicates the work duration of the interval timer, in milliseconds, which cannot be less than the
interval. When the work duration is over, IntervalTimer#onFinish() will be called to notify
that the interval timer has gone off.interval - Indicates the interval time, in milliseconds, which is within the range of
(1, duration].
Every time an interval starts, IntervalTimer#onInterval(long) is called to notify the remaining work time
of the interval timer.public final void schedule()
IllegalArgumentException - Throws this exception if the interval is less than or equal to 0,
or greater than the work duration.public final void cancel()
public abstract void onInterval(long remain)
remain - Indicates the remaining work time of the interval timer.
The value may be duplicated due to precision.public abstract void onFinish()