Three implementation methods of timer in Android

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.

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:

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:

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

HandlerHandler =New Handler ();

Runnable = New Runnable (){ @ Override Public Void Run (){ // Todo auto-generated method stub // What to do Handler. postdelayed ( This , 2000 ); } }; 2. Start the timer handler. postdelayed (runnable, 2000 ); // Run runnable every two seconds 3. stop the timer handler. removecallbacks (runnable ); 3. Handler, timer, and timertask

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

PrivateFinal 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 the timer 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. 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.

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.