3 ways to implement timer in Android _android

Source: Internet
Author: User
Tags stub thread class

In Android development, timers typically have the following 3 implementation methods:
a sleep (long) method using handler and thread
Second, the postdelayed (Runnable, long) method using handler
Third, the use of handler and timer and timertask combination of methods
here is a description:
a sleep (long) method using handle and thread
Handler is primarily used to process received messages. This is only the main method, of course, there are other methods in handler for implementation, interested in the use of the API, here do not explain too much.
1. Define a handler class for handling the received message.

Handler Handler = new Handler () {public
  void Handlemessage (Message msg) {
    //What to do
    super.handlemessage (msg) ;
  }
};

2. Create a new thread class that implements the Runnable interface, as follows:

public class Mythread implements Runnable {
 @Override public
 void Run () {
 //TODO auto-generated method stub
   while (True) {
  try {
  thread.sleep (10000);/thread pauses 10 seconds, unit millisecond message
  = new Message ();
  Message.what = 1;
  Handler.sendmessage (message);//Send Messages
  } catch (Interruptedexception e) {
  //TODO auto-generated catch block< C12/>e.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, the postdelayed (Runnable, long) method using handler
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 a stub
    Things to do
    handler.postdelayed (this,);
  }
;

2. Start Timer
handler.postdelayed (runnable, 2000);//runnable once every two seconds.
3. Stop Timer
handler.removecallbacks (runnable);

Third, the use of handler and timer and timertask combination of methods
1. Define timer, timer task and handler handle

Private Final Timer timer = new timer (); 
Private TimerTask task; 
Handler Handler = new Handler () { 
  @Override public 
  void Handlemessage (msg) { 
    //TODO Auto-generate D method Stub 
    //To do things 
    super.handlemessage (msg); 
  }
;

2. Initialize Timer task

task = new TimerTask () { 
  @Override public 
  void Run () { 
    //TODO auto-generated a stub message 
    mess Age = New Message (); 
    Message.what = 1; 
    Handler.sendmessage (message); 
  } 
; 

3. Start Timer
Timer.schedule (Task, 2000, 2000);
Briefly describe some of the things mentioned in the previous three steps:
1. Timer task (TimerTask) as the name suggests, that is, when the timer arrives at a specified time to do the work, here is to handler send a message, by the handler class to handle.
2. Java.util.Timer.schedule (timertask task, long delay): This method means that the task is executed after dalay/1000 seconds. Only one execution.
Java.util.Timer.schedule (timertask task, long delay, long period): This method is to say, delay/1000 seconds after the task, and then entered the period/ 1000 seconds to execute the task again, this is used to cycle tasks, execute countless times, of course, you can use Timer.cancel ();

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.