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.
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:
Public
Class
Mythread
Implements
Runnable {
@ Override
Public
Void
Run (){
// Todo auto-generated method stub
While
(
True
){
Try
{
Thread. Sleep (
10000
);
// Pause the thread 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:
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
Handler
Handler =New
Handler ();
Runnable =
New
Runnable (){
@ Override
Public
Void
Run (){
// Todo auto-generated method stub
// What to do
Handler. postdelayed (
This
,
2000
);
}
};
2. Start the timer handler. postdelayed (runnable,
2000
);
// Run runnable every two seconds
3. stop the timer handler. removecallbacks (runnable );
3. Handler, timer, and timertask
1. Define the 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
// What to do
Super
. Handlemessage (MSG );
}
};
2. initialize the timer 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. Schedule (task,2000
,
2000
);
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.