3 Solutions for implementing timers based on Android _android

Source: Internet
Author: User
In Android development, timers typically have the following 3 implementation methods:
a sleep (long) method using handler and thread
Second, the postdelayed (Runnable, long) method using handler
Third, the use of handler and timer and timertask combination of methods
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.
Copy Code code as follows:

Handler Handler = new Handler () {
public void Handlemessage (msg) {
Things to do
Super.handlemessage (msg);
}
};

2. Create a new thread class that implements the Runnable interface, as follows:
Copy Code code 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 messages
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}

3. Add the following statement where you want to start the thread:
Copy Code code as follows:

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
Copy Code code as follows:

Handler handler=new Handler ();
Runnable runnable=new Runnable () {
@Override
public void Run () {
TODO auto-generated Method Stub
Things to do
Handler.postdelayed (this, 2000);
}
};

2. Start timer
Copy Code code as follows:

Handler.postdelayed (runnable, 2000)//runnable every two seconds

3. Stop Timer
Copy Code code as follows:

Handler.removecallbacks (runnable);

Third, the use of handler and timer and timertask combination of methods
1. Define timer, timer task and handler handle
Copy Code code as follows:

Private Final Timer timer = new timer ();
Private TimerTask task;
Handler Handler = new Handler () {
@Override
public void Handlemessage (msg) {
TODO auto-generated Method Stub
Things to do
Super.handlemessage (msg);
}
};

2. Initialize Timer task
Copy Code code 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 Timer
Copy Code code as follows:

Timer.schedule (Task, 2000, 2000);

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

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.