3 Implementation methods for timed tasks in Android (recommended) _android

Source: Internet
Author: User
Tags stub thread class

In Android development, there are 3 ways to implement tasks on a regular basis:

First, the use of handler and thread sleep (long) method (not recommended, Java implementation)

Second, the use of handler postdelayed (Runnable, Long) method (the simplest Android implementation)

Third, the use of handler and timer and TimerTask combined method (more tasks recommended)

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 (msg) { 
    //The thing 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 = new Message (); 
    Message.what = 1; 
    Handler.sendmessage (message); 
  } 
;  

3. Start Timer

Timer.schedule (Task, 2000, 2000);

4. Stop Timer

Timer.cancel ();

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 ();

Each timer only corresponds to a single thread.

A timer does not guarantee that the task performs very accurately.

The Timer class is thread-safe.

The above is a small series of Android for everyone to carry out the task of 3 kinds of implementation methods (recommended) All content, I hope that we support 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.