Class TaskScheduler
- java.lang.Object
-
- eu.woolplatform.utils.schedule.TaskScheduler
-
- Direct Known Subclasses:
DefaultTaskScheduler
@AppComponent public abstract class TaskScheduler extends Object
The task scheduler can be used to schedule one-time or repeating tasks to be run at specific times. It has an implementation for Android and for standard Java.If you schedule background tasks in Android, you should be aware that Android devices may kill the app process in the background. As long as the app process is running, the task scheduler will keep scheduled tasks in memory so they can be run at the scheduled time. But if the app is killed, the scheduled task needs to be reconstructed at the scheduled time. This is done using a data string that is obtained from the
ScheduledTaskmethodgetTaskData(). The task is rebuilt with these steps:- A new instance of the same class of
ScheduledTaskis constructed. This is done using a constructor that takes one Context parameter (in Android) or the default constructor. - The method
setTaskData()is called.
If you schedule a task that may need to be reconstructed, make sure that the methods
getTaskData()andsetTaskData()are implemented, and that theScheduledTaskclass has an accessible default constructor or a constructor that takes one Android Context parameter. In general try to do this for any task with another schedule thanTaskSchedule.Immediate. You can omit it if the task is scheduled while the app is in the foreground (e.g. an activity or fragment is resumed) and it's cancelled when the app goes to the background (an activity or fragment is paused).
-
-
Constructor Summary
Constructors Constructor Description TaskScheduler()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voidcancelScheduledTask(Object context, String taskId)Cancels the scheduled task with the specified task ID.voidcancelTask(Object context, String taskId)Cancels the scheduled or running task with the specified task ID.voidcancelTasksWithClass(Object context, Class<? extends ScheduledTask> taskClass)Cancels any scheduled or running tasks that are an instance of the specified task class.protected abstract booleancanRunTaskOnMainThread()Returns whetherisRunOnWorkerThread()should be checked to see if a task should be run on the main thread or on the worker thread.<T extends ScheduledTask>
List<T>findTasksWithClass(Class<T> taskClass)Finds any scheduled or running tasks that are an instance of the specified task class.StringgenerateTaskId()Generates a random task ID.voidinitScheduledTasks(Object context, List<ScheduledTaskSpec> taskSpecs)Initializes the tasks that were scheduled at a previous run and that have not been triggered or cancelled yet.voidonTriggerTask(Object context, ScheduledTaskSpec taskSpec)Called when a scheduled task should be run.protected abstract voidrunOnUiThread(Runnable runnable)Runs code on the UI thread in Android.voidscheduleTask(Object context, ScheduledTask task, String taskId)Schedules a task.protected abstract voidscheduleTask(Object context, ScheduledTaskSpec taskSpec)Schedules a task at a specified time.
-
-
-
Field Detail
-
LOGTAG
public static final String LOGTAG
-
-
Method Detail
-
initScheduledTasks
public void initScheduledTasks(Object context, List<ScheduledTaskSpec> taskSpecs)
Initializes the tasks that were scheduled at a previous run and that have not been triggered or cancelled yet. In Android this method is called on the UI thread.- Parameters:
context- the context (only set in Android)taskSpecs- the tasks
-
findTasksWithClass
public <T extends ScheduledTask> List<T> findTasksWithClass(Class<T> taskClass)
Finds any scheduled or running tasks that are an instance of the specified task class. It returns a map from task ID toScheduledTask.- Type Parameters:
T- the type of task- Parameters:
taskClass- the task class- Returns:
- the tasks
-
generateTaskId
public String generateTaskId()
Generates a random task ID.- Returns:
- the task ID
-
scheduleTask
public void scheduleTask(Object context, ScheduledTask task, String taskId)
Schedules a task. See the discussion at the top of this page about rebuilding tasks. In Android this method is called on the UI thread.- Parameters:
context- the context (only set in Android)task- the tasktaskId- the task ID
-
cancelTask
public void cancelTask(Object context, String taskId)
Cancels the scheduled or running task with the specified task ID. In Android this method is called on the UI thread.- Parameters:
context- the context (only set in Android)taskId- the task ID
-
cancelTasksWithClass
public void cancelTasksWithClass(Object context, Class<? extends ScheduledTask> taskClass)
Cancels any scheduled or running tasks that are an instance of the specified task class.- Parameters:
context- the contexttaskClass- the task class
-
scheduleTask
protected abstract void scheduleTask(Object context, ScheduledTaskSpec taskSpec)
Schedules a task at a specified time. At the due time it should callonTriggerTask(). In Android this method is called on the UI thread.- Parameters:
context- the context (only set in Android)taskSpec- the specification of the task instance
-
cancelScheduledTask
protected abstract void cancelScheduledTask(Object context, String taskId)
Cancels the scheduled task with the specified task ID. In Android this method is called on the UI thread.- Parameters:
context- the context (only set in Android)taskId- the task ID
-
runOnUiThread
protected abstract void runOnUiThread(Runnable runnable)
Runs code on the UI thread in Android. In standard Java, the code can be run immediately on the calling thread.- Parameters:
runnable- the code to run
-
canRunTaskOnMainThread
protected abstract boolean canRunTaskOnMainThread()
Returns whetherisRunOnWorkerThread()should be checked to see if a task should be run on the main thread or on the worker thread. This is used in Android to run tasks on the UI thread. If this method returns false, a task is always run on a worker thread.- Returns:
- true if a task can be run on the main thread, false otherwise
-
onTriggerTask
public void onTriggerTask(Object context, ScheduledTaskSpec taskSpec)
Called when a scheduled task should be run. In Android this method is called on the UI thread.- Parameters:
context- the context (only set in Android)taskSpec- the specification of the task instance
-
-