Class Timer
Schedules one-shot or recurring tasks for execution.
You have to extend this abstract class and implement the method onTimeout().
The timer runs inside the Android main UI, so there will be no problems when using UI elements from the timer's onTimeout() method. If the UI main thread is busy, timer execution may be subject to delays.
One-shot timers are scheduled to run after a relative delay.
Periodically timers are scheduled with a fixed period and the very first time they are executed immediately.
This class does not offer guarantees about the real-time nature of task scheduling. If you want to animate LEDs or digital outputs you might consider to
use the class Animation
which offers a very simple API to construct and run animations.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract void
This method will be called either once after the specified delay or periodically whenever the timer expires.boolean
ready()
If this method returns true, it is save to schedule the timer.void
scheduleOnce
(int delayMs) Schedules the timer once to expire after the given delay.void
schedulePeriodically
(int intervalMs) Schedules the timer continuously using the given interval.void
stop()
Stops/cancels/pauses the pending timer.
-
Constructor Details
-
Timer
public Timer()
-
-
Method Details
-
onTimeout
public abstract void onTimeout()This method will be called either once after the specified delay or periodically whenever the timer expires.
-
scheduleOnce
public void scheduleOnce(int delayMs) Schedules the timer once to expire after the given delay.
Note that if the timer has already scheduled, this method will do nothing at all.
- Parameters:
delayMs
- The delay in milliseconds.
-
schedulePeriodically
public void schedulePeriodically(int intervalMs) Schedules the timer continuously using the given interval. Note that the first time the timer expires immediately.
- Parameters:
intervalMs
- Interval between the points in time the timer expires.
-
stop
public void stop()Stops/cancels/pauses the pending timer. If the timer was scheduled once and did not already expire, it will canceled. Periodically timers will be paused.
You can reschedule timers, but you need to wait until the ready() method returns true.
-
ready
public boolean ready()If this method returns true, it is save to schedule the timer.
- Returns:
- True if the timer is ready to be rescheduled.
-