In Android development, the timer generally has the following 3 ways to implement:
First, the use of handler and thread of sleep (long) method;
Second, the use of handler postdelayed (Runnable, long) method;
Third, the use of handler and timer and timertask combination method;
The sleep (long) method using handle and thread
Handler is primarily used to handle received messages. This is only the main method, of course, there are other ways to achieve handler, interested in the API can be checked, there is not too much explanation.
1. Define a handler class to handle the received message.
Handler Handler = new Handler () {public void Handlemessage (Message msg) { //things to do super.handlemessage (msg) ; }};
2. Create a new thread class that implements the Runnable interface, as follows:
public class MyThread implements Runnable {@Overridepublic void Run () {//TODO auto-generated method Stubwhile (true) {try {Thread.Sleep (10000);//thread pauses 10 seconds, unit milliseconds Message message = new Message (); message.what = 1;handler.sendmessage (message);// Send Message} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
3. Add the following statement where you want to start the thread:
New Thread (New MyThread ()). Start ();
4. After the thread is started, the thread sends a message every 10s.
Second, using handler postdelayed (Runnable, Long) method
This implementation is relatively simple.
1. Define a handler class
Handler handler=new Handler (); Runnable runnable=new Runnable () { @Override public void Run () { //TODO auto-generated Method stub // What to do handler.postdelayed (this, a); }};
2. Start the timer
3. Stop Timer
Handler.removecallbacks (runnable);
Third, the use of handler and timer and TimerTask combination method
A timer is a timer tool that is used to schedule a specified task in a background thread. It can schedule a task to execute once or repeatedly.
TimerTask an abstract class whose subclasses represent a task that can be scheduled by the timer.
The timer and TimerTask can be used as the third way to implement the thread, the first two methods are inherited from the thread class and implement the Runnable interface respectively.
A timer is a thread facility that is used to schedule tasks that are later performed in a background thread. The task can be scheduled to execute once, or periodically repeated execution, can be regarded as a timer, can dispatch timertask. TimerTask is an abstract class that implements the Runnable interface, so it has the capability of multithreading.
Import Java.util.Timer; Import Java.util.TimerTask; public class Timertest { static class MyTimerTask1 extends TimerTask {public void run () { System.out.println ("Explosion!!! "); } } public static void Main (string[] args) { Timer timer = new timer (); Timer.schedule (New MyTimerTask1 (), 5000);//5 Seconds to start the task } }
Schedule is a timer scheduling task, the timer reconstructs four schedule methods, you can view the JDK API.
3 Ways to implement timers in Android