Three solutions for timer implementation based on 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.
Copy codeThe Code is as follows: 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:
Copy codeThe Code is as follows: public class MyThread implements Runnable {
@ Override
Public void run (){
// TODO Auto-generated method stub
While (true ){
Try {
Thread. sleep (10000); // Thread pause 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:
Copy codeThe Code is as follows: 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
Copy codeThe Code is as follows: 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
Copy codeThe Code is as follows: handler. postDelayed (runnable, 2000); // run runnable every two seconds.

3. stop the timer.
Copy codeThe Code is as follows: handler. removeCallbacks (runnable );

3. Handler, timer, and TimerTask
1. Define the timer, timer task, and Handler handle
Copy codeThe Code is as follows: 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
Copy codeThe Code is as follows: 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
Copy codeThe Code is as follows: timer. schedule (task, 2000,200 0 );

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.