- A simple timer task handler based on android.os.Handler - You can decide when to execute tasks.
- Author: Luo Guowen
- Email: luoguowen123@qq.com
-
Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
Step 2. Add the dependency:
dependencies { compile 'com.github.lgw666:SimpleTimerTaskHandler:v1.0' }
-
Step 1. Get the instance of SimpleTimerTaskHandler
SimpleTimerTaskHandler handler = SimpleTimerTaskHandler.getInstance();
Step 2. Create your task
SimpleTimerTask task = new SimpleTimerTask() { @Override public void run() { // Do what you want } };
or
SimpleTimerTask loopTask = new SimpleTimerTask(loop interval) { @Override public void run() { // Do what you want } };
Tip: the task will be a loop task once you use the second constructor.
Step 3. Execute your task by using SimpleTimerTaskHandler
Execute real-time task
void sendTask(int taskNum, SimpleTimerTask task);
Execute delay task
void sendTaskDelayed(int taskNum, SimpleTimerTask task, long delayMillis);
Execute timer task, accurate to hour.
void sendTimerTask(int taskNum, SimpleTimerTask task, int hour);
Execute timer task, accurate to minute.
void sendTimerTask(int taskNum, SimpleTimerTask task, int hour, int minute);
Execute timer task, accurate to second.
void sendTimerTask(int taskNum, SimpleTimerTask task, int hour, int minute, int second);
-
Task id, eg: 0, 1, 2...
int taskNum;
The task you want to execute.
SimpleTimerTask task;
The hour to execute this task, based on the day.
int hour
The minute in the hour, based on the day.
int minute
The seconds in the minute, based on the day.
int second
e.g.: If you want execute a task at 13:50:30, you can invoke
handler.sendTimerTask(task num, task, 13, 50, 30);
-
Must use SimpleTimerTaskHandler and SimpleTimerTask together.
Public constructors
Default constructor associates this task.
SimpleTimerTask();
Constructor associates this task change its task type from default to loop, and sets the loop interval.
SimpleTimerTask(long loopInterval);
-
- The task number of every task should be different if there are more than one tasks.
- You`d better use a work Thread or AsyncTask to do some long time operations, e.g: network request.
- You can do UI operation in the task