TimeTask is a lightweight and concise scheduled task queue framework, which focuses on multi-group task distribution. The internal implementation of TimeTask is based on AlarmManager+ broadcast, which is well encapsulated between task and system API

In simple terms, it meets the following application scenarios:

  • 1. When you need to start and stop tasks on a regular basis
  • 2. You have multiple sets of tasks, and your timelines may overlap

Current application scenarios:

  • 1. TV set-top box media distribution
  • 2. Android large screen advertising machine task rotation

The introduction of

Add to root Gradle

	repositories {
			...
			maven { url 'https://jitpack.io' }
		}
Copy the code
Dependencies {the compile 'com. Making. BolexLiu: TimeTask: 1.0'}Copy the code

Simple to use

1. Define a Task as your Task object. Note that the base Task already contains the start time and end time of the Task

Class MyTask extends Task {//// TODO: this is where you can put your own resources. }Copy the code

2. Define a task sink

TimeHandler<MyTask> TimeHandler = new TimeHandler<MyTask>() {@override public void exeTask(MyTask mTask) {// To execute // In general, handling your logic in exeTask is fine, } @override public void overdueTask(MyTask mTask) {// Override public void futureTask(MyTask mTask) {// will execute}};Copy the code

3. Define a task dispatcher and add a sink

TimeTask<MyTask> myTaskTimeTask = new TimeTask<>(MainActivity.this); / / create a task processor myTaskTimeTask addHandler (timeHandler); // Add time backCopy the code

4. Configure your task interval, (start time, end time)

private List<MyTask> creatTasks() { return new ArrayList<MyTask>() {{ MyTask BobTask = new MyTask(); BobTask.setStarTime(System.currentTimeMillis()); // Bobtask.setendTime (system.currentTimemillis ()+5*1000); // End after 5 seconds bobtask. name="Bob"; add(BobTask); MyTask benTask = new MyTask(); benTask.setStarTime(System.currentTimeMillis()+10*1000); Bentask.setendtime (System.currentTimemillis ()+15*1000); bentask.setendTime (System.currentTimemillis ()+15*1000); Bentask. name="Ben"; add(benTask); }}; }Copy the code

5. Add your task queue and run.

myTaskTimeTask.setTasks(creatTasks()); / / create time task resource Put the resources in processing myTaskTimeTask. StartLooperTask (); / / startCopy the code

. So, when the myTaskTimeTask startLooperTask (after), will be distributed to timeHandler name is Bob’s task. Then 10 seconds to distribute the Ben name of the task. The task processor distributes work based on our configured start time and end time.

Refer to the examples in the app for the complete code.

Api

TimeTask

  • TimeTask(Context mContext); / / initialization
  • setTasks(List mES); // Set the task list
  • addHandler(TimeHandler mTH); // Add task listener
  • startLooperTask(); // Start the task
  • stopLooper(); // Stop the task
  • spotsTask(List mSpotsTask); // The task is interrupted
  • onColse(); // Close to prevent memory leaks

Code has detailed comments, read the principle of the best code.

Note:

  • 1. Make sure the tasks in your task queue are sorted by time.
  • 2. Be sure to use generics to inherit Task tasks.

The underlying principle of the subsequent analysis of the article.