Original: http://blog.csdn.net/wulianghuan/article/details/8507221
In Android development, the timer generally has the following 3 ways to implement:
The sleep (long) method using handler and thread
Second, using handler postdelayed (Runnable, Long) method
Third, the use of handler and timer and TimerTask combination method
Here are the following:
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.
?
| 123456 |
Handler handler = newHandler() { public voidhandleMessage(Message msg) { // 要做的事情 super.handleMessage(msg); }}; |
2. Create a new thread class that implements the Runnable interface, as follows:
?
| 1234567891011121314151617 |
publicclassMyThread implementsRunnable { @Override publicvoidrun() { // TODO Auto-generated method stub while(true) { try{ Thread.sleep(10000);// 线程暂停10秒,单位毫秒 Message message = newMessage(); message.what = 1; handler.sendMessage(message);// 发送消息 } catch(InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }} |
3. Add the following statement where you want to start the thread:
?
| 1 |
newThread(newMyThread()).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
?
| 123456789 |
Handler handler=new Handler();Runnable runnable=newRunnable() { @Override publicvoidrun() { // TODO Auto-generated method stub //要做的事情 handler.postDelayed(this, 2000); }}; |
2. Start the timer
?
| 1 |
handler.postDelayed(runnable, 2000);//每两秒执行一次runnable. |
3. Stop Timer
?
| 1 |
handler.removeCallbacks(runnable); |
Third, the use of handler and timer and TimerTask combination method
1. Define timer, timer task and handler handle
?
| 12345678910 |
privatefinalTimer timer = newTimer();privateTimerTask task;Handler handler = new Handler() { @Override publicvoidhandleMessage(Message msg) { // TODO Auto-generated method stub // 要做的事情 super.handleMessage(msg); }}; |
2. Initializing Timer tasks
?
| 123456789 |
task = newTimerTask() { @Override publicvoidrun() { // TODO Auto-generated method stub Message message = newMessage(); message.what = 1; handler.sendMessage(message); }}; |
3. Start the timer
?
| 1 |
timer.schedule(task, 2000, 2000); |
Briefly describe some of the three steps mentioned above:
1. Timer task (TimerTask) as the name implies, that is, when the timer arrives at the specified time to do the work, here is to handler send a message, by the handler class for processing.
2. Java.util.Timer.schedule (timertask task, long delay): This method means that the task is executed after dalay/1000 seconds. Only once.
Java.util.Timer.schedule (timertask task, long delay, long period): This method is to say that the task is executed after delay/1000 seconds and then into period/ 1000 seconds to execute the task again, this is used for looping tasks, executed countless times, of course, you can use Timer.cancel (); Cancel the execution of the timer.
3 ways to implement timer in Android