LooperDelegatingScheduler
public class Scheduler extends Object
The execution of a scheduler can be in one of three states:
pause()
): if paused, then no posted events will be run unless the
Scheduler is explicitly instructed to do so, correctly matching Android's behavior.
unPause()
): if not paused but not set to idle constantly, then the
Scheduler will automatically run any Runnable
s that are scheduled to run at or
before the Scheduler's current time, but it won't automatically run any future events. To
run future events the Scheduler needs to have its clock advanced.
idleConstantly(boolean)
is called with true, then the
Scheduler will continue looping through posted events (including future events), advancing
its clock as it goes.
Modifier and Type | Class | Description |
---|---|---|
static class |
Scheduler.IdleState |
Describes the current state of a
Scheduler . |
Constructor | Description |
---|---|
Scheduler() |
Modifier and Type | Method | Description |
---|---|---|
boolean |
advanceBy(long interval) |
Deprecated.
|
boolean |
advanceBy(long amount,
TimeUnit unit) |
Run all runnables that are scheduled to run in the next time interval.
|
boolean |
advanceTo(long endTime) |
Run all runnables that are scheduled before the endTime.
|
boolean |
advanceToLastPostedRunnable() |
Run all runnables in the queue, and any additional runnables they schedule that are scheduled
before the latest scheduled runnable currently in the queue.
|
boolean |
advanceToNextPostedRunnable() |
Run the next runnable in the queue.
|
boolean |
areAnyRunnable() |
Determine if any enqueued runnables are enqueued before the current time.
|
long |
getCurrentTime() |
Get the current time (as seen by the scheduler), in milliseconds.
|
Scheduler.IdleState |
getIdleState() |
Retrieves the current idling state of this
Scheduler . |
Duration |
getLastScheduledTaskTime() |
|
Duration |
getNextScheduledTaskTime() |
|
void |
idleConstantly(boolean shouldIdleConstantly) |
Deprecated.
This method is ambiguous in how it should behave when turning off constant idle.
Use
setIdleState(IdleState) instead to explicitly set the state. |
boolean |
isPaused() |
Determine if the scheduler is paused.
|
void |
pause() |
Pause the scheduler.
|
void |
post(Runnable runnable) |
Add a runnable to the queue.
|
void |
postAtFrontOfQueue(Runnable runnable) |
Add a runnable to the head of the queue.
|
void |
postDelayed(Runnable runnable,
long delayMillis) |
Add a runnable to the queue to be run after a delay.
|
void |
postDelayed(Runnable runnable,
long delay,
TimeUnit unit) |
Add a runnable to the queue to be run after a delay.
|
void |
remove(Runnable runnable) |
Remove a runnable from the queue.
|
void |
reset() |
Reset the internal state of the Scheduler.
|
boolean |
runOneTask() |
Run the next runnable in the queue.
|
void |
setIdleState(Scheduler.IdleState idleState) |
Sets the current idling state of this
Scheduler . |
int |
size() |
Return the number of enqueued runnables.
|
void |
unPause() |
Un-pause the scheduler.
|
public Scheduler.IdleState getIdleState()
Scheduler
.Scheduler
.setIdleState(IdleState)
,
isPaused()
public void setIdleState(Scheduler.IdleState idleState)
Scheduler
. If transitioning to the Scheduler.IdleState.UNPAUSED
state any tasks scheduled to be run at or before the current time will be
run, and if transitioning to the Scheduler.IdleState.CONSTANT_IDLE
state all scheduled tasks will
be run and the clock advanced to the time of the last runnable.idleState
- The new idle state of this Scheduler
.setIdleState(IdleState)
,
isPaused()
public long getCurrentTime()
public void pause()
setIdleState(PAUSED)
.unPause()
,
setIdleState(IdleState)
public void unPause()
setIdleState(UNPAUSED)
.pause()
,
setIdleState(IdleState)
public boolean isPaused()
public void post(Runnable runnable)
runnable
- Runnable to add.public void postDelayed(Runnable runnable, long delayMillis)
runnable
- Runnable to add.delayMillis
- Delay in millis.public void postDelayed(Runnable runnable, long delay, TimeUnit unit)
public void postAtFrontOfQueue(Runnable runnable)
runnable
- Runnable to add.public void remove(Runnable runnable)
runnable
- Runnable to remove.public boolean advanceToLastPostedRunnable()
public boolean advanceToNextPostedRunnable()
@Deprecated public boolean advanceBy(long interval)
advanceBy(long, TimeUnit)
.interval
- Time interval (in millis).public boolean advanceBy(long amount, TimeUnit unit)
public boolean advanceTo(long endTime)
endTime
- Future time.public boolean runOneTask()
public boolean areAnyRunnable()
public void reset()
public int size()
public Duration getNextScheduledTaskTime()
public Duration getLastScheduledTaskTime()
@Deprecated public void idleConstantly(boolean shouldIdleConstantly)
setIdleState(IdleState)
instead to explicitly set the state.shouldIdleConstantly
- If true the idle state will be set to Scheduler.IdleState.CONSTANT_IDLE
, otherwise it will be set to Scheduler.IdleState.UNPAUSED
.