In Android development, the timer generally has the following three implementation methods:

Source: Internet
Author: User

In Android development, the timer generally has the following three implementation methods:

I. Use the handler and thread sleep (long) Methods
2. Use the postdelayed (runnable, long) method of handler
3. Handler, timer, and timertask

Next we will introduce them one by one:

I. Use the handle and thread sleep (long) Methods

Handler is mainly used to process received messages. This is only the main method. Of course, there are other methods in handler for implementation. If you are interested, you can check the API. I will not explain it here.

1. Define a handler class to process received messages.

?
123456 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 shown below:

?
1234567891011121314151617 public
class
MyThread implements
Runnable {     @Override    public
void run() {         // TODO Auto-generated method stub
        while
(true) {
            try
{                 Thread.sleep(10000);// Pause the thread for 10 seconds, in milliseconds
                Message message =
new Message();                 message.what =
1;                 handler.sendMessage(message);// Send a message
            }
catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

3. Add the following statement to the thread to be started:

?
1 new
Thread(
new MyThread()).start();

4. After the thread is started, the thread sends a message every 10 seconds.

2. Use the postdelayed (runnable, long) method of handler

This implementation is simpler.

1. Define a handler class

?
123456789 Handler handler=new
Handler(); Runnable runnable=new
Runnable() {     @Override    public
void run() {         // TODO Auto-generated method stub
        // What to do
        handler.postDelayed(this,
2000);     }
};

2. Start the timer

?
1 handler.postDelayed(runnable,
2000);// Run runnable every two seconds.

3. stop the timer.

?
1 handler.removeCallbacks(runnable);

3. Handler, timer, and timertask

1. Define the timer, timer task, and handler handle

?
1234567891011    private
final
Timer timer = new
Timer();  private
TimerTask task; 
Handler handler =
new Handler() {      @Override     public
void handleMessage(Message msg) { 
        // TODO Auto-generated method stub 
        // What to do
        super.handleMessage(msg); 
    }
};

2. initialize a timer task

?
123456789 task = new
TimerTask() {      @Override     public
void run() {          // TODO Auto-generated method stub 
        Message message =
new Message(); 
        message.what =
1;          handler.sendMessage(message); 
    } 
};

3. Start the timer

?
1 timer.schedule(task,
2000, 2000);

Let's briefly introduce some of the content mentioned in the above three steps:

1. As the name suggests, the timer task is the task to be done when the timer reaches the specified time. Here, the handler class wants to send a message for processing.

2. java. util. Timer. Schedule (timertask task, long delay): This method means that dalay/1000 seconds later will execute the task only once.
Java. util. timer. schedule (timertask task, long delay, long period): This method means that the task is executed after delay/1000 seconds, and then executed again after period/1000 seconds, this is used to execute cyclic tasks countless times. Of course, you can use timer. cancel (); cancel timer execution.

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.