- 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
SimpleTimerTasktask = newSimpleTimerTask() { @Overridepublicvoidrun() { // Do what you want } };
or
SimpleTimerTaskloopTask = newSimpleTimerTask(loopinterval) { @Overridepublicvoidrun() { // 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
voidsendTask(inttaskNum, SimpleTimerTasktask);
Execute delay task
voidsendTaskDelayed(inttaskNum, SimpleTimerTasktask, longdelayMillis);
Execute timer task, accurate to hour.
voidsendTimerTask(inttaskNum, SimpleTimerTasktask, inthour);
Execute timer task, accurate to minute.
voidsendTimerTask(inttaskNum, SimpleTimerTasktask, inthour, intminute);
Execute timer task, accurate to second.
voidsendTimerTask(inttaskNum, SimpleTimerTasktask, inthour, intminute, intsecond);
Task id, eg: 0, 1, 2...
inttaskNum;
The task you want to execute.
SimpleTimerTasktask;
The hour to execute this task, based on the day.
inthour
The minute in the hour, based on the day.
intminute
The seconds in the minute, based on the day.
intsecond
e.g.: If you want execute a task at 13:50:30, you can invoke
handler.sendTimerTask(tasknum, 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(longloopInterval);
- 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