3 ways to implement timed tasks in Android

Source: Internet
Author: User

In Android development, there are 3 ways to implement a scheduled task:


The sleep (long) method with handler and thread (not recommended, Java implementation)
Second, using handler postdelayed (Runnable, Long) method (the simplest Android implementation)
Third, the use of handler and timer and timertask combination of methods (more tasks recommended)
Here are the following:
The sleep (long) method using handle and thread
Handler is primarily used to handle received messages. This is only the main method, of course, there are other ways to achieve handler, interested in the API can be checked, there is not too much explanation.

1. Define a handler class to handle the received message.

Handler Handler = new Handler () {
public void Handlemessage (Message msg) {
The things 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 paused for 10 seconds, per millisecond
Message message = new Message ();
Message.what = 1;
Handler.sendmessage (message);//Send Message
} catch (Interruptedexception e) {
TODO auto-generated Catch block
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, using handler postdelayed (Runnable, Long) method
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 Method Stub
The things to Do
Handler.postdelayed (this, 2000);
}
};

2. Start the timer

Handler.postdelayed (Runnable, 2000);//Perform runnable once every two seconds.

3. Stop Timer

Handler.removecallbacks (runnable);

Third, the use of handler and timer and TimerTask combination method
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 (Message msg) {
TODO auto-generated Method Stub
The things to Do
Super.handlemessage (msg);
}
};

2. Initializing Timer tasks

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

Timer.schedule (Task, 2000, 2000);

4. Stop Timer

Timer.cancel ();

Briefly describe some of the three steps mentioned above:
1. Timer task (TimerTask) as the name implies, that is, when the timer arrives at the specified time to do the work, here is to handler send a message, by the handler class for processing.
2. Java.util.Timer.schedule (timertask task, long delay): This method means that the task is executed after dalay/1000 seconds. Only once. Java.util.Timer.schedule (timertask task, long delay, long period): This method is to say that the task is executed after delay/1000 seconds and then into period/ 1000 seconds to execute the task again, this is used for looping tasks, executed countless times, of course, you can use Timer.cancel (); Cancel the execution of the timer.
Each timer only corresponds to a single thread. The timer does not guarantee that the task executes very precisely. The Timer class is thread-safe.

3 ways to implement timed tasks in Android

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.