Class 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 ScheduledTask method getTaskData(). The task is rebuilt with these steps:

    • A new instance of the same class of ScheduledTask is 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() and setTaskData() are implemented, and that the ScheduledTask class 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 than TaskSchedule.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).

    • Field Detail

      • LOGTAG

        public static final String LOGTAG
    • Constructor Detail

      • TaskScheduler

        public TaskScheduler()
    • 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 ScheduledTaskList<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 to ScheduledTask.
        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 task
        taskId - 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 context
        taskClass - 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 call onTriggerTask(). 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 whether isRunOnWorkerThread() 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