Android Timer implementation method [GO]

Source: Internet
Author: User

in theAndroid, frequently used timers are mainly implemented in the following ways:
One, adopted handler with thread sleep (long) method
Ii. >handler with style= font-family " postdelayed (Runnable, long) method
III, with handler and and timertask Span style= "font-family: Song Body" > method of combining.
here are the following:
The Sleep (long) method using Handle and thread
HandlerIt is used primarily to handle received messages. It's just the main way, of course .HandlerThere are other ways to do it, and it's interesting to find out .API, not too much to explain here.
1.Define aHandlerclass that is used to handle the receivedMessage.
Handler Handler = new Handler () {
public void Handlemessage (Message msg) {
//the things to do
Super.handlemessage (msg);
}
};
2.Create a new implementationRunnablethe thread class for the interface. as follows:
public class MyThread implements runnable{
@Override
public void Run () {
TODO auto-generated Method Stub
while (true) {
try {
Thread.Sleep (10000);//Thread pausedTenseconds, 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 need to start the thread:
New Thread (New MyThread ()). Start ();
4.after the thread is started, the thread each10ssends a message once.
Second, using Handler postdelayed (Runnable, long) method
This implementation is relatively simple:
1. 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 Timer:
Handler.postdelayed (runnable,);//executes once every two secondsRunnable.
3.Stop timer:
Handler.removecallbacks (runnable);
Third, the use of Handler and timer and timertask combination method.
1.define timers, timer tasks andHandler 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.initializes the timer task.
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
Timer.schedule (Task, 2000, 2000);
briefly describe some of the things mentioned in the three steps above.
1.Timer Task (TimerTaskAs the name implies, that is, when the timer arrives at the specified time to do the work, here is toHandlersend a message, byHandlerclass for processing.
2. Java.util.Timer.schedule (timertask task, long delay):This approach is to say thatdalay/1000seconds after executiontask.executes only once.
Java.util.Timer.schedule (timertask task, long delay, long period): This method is to say thatdelay/1000seconds after executiontask,and then in.period/1000seconds to execute againTask, this is used for cyclic tasks, executed countless times, and of course, you can useTimer.cancel ();cancels the execution of the timer.

Android Timer implementation method [GO]

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.