In android, timers are commonly used in the following ways:
I. Use the Handler and thread sleep (long) Methods
2. Use the postDelayed (Runnable, long) method of Handler
3. Handler is used in combination with 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.
[Java]
<SPAN style = "COLOR: #993300"> Handler handler = new Handler (){
Public void handleMessage (Message msg ){
// What to do
Super. handleMessage (msg );
}
};
</SPAN>
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 follows:
[Java]
<SPAN style = "COLOR: #993300"> public class MyThread implements Runnable {
@ Override
Public void run (){
// TODO Auto-generated method stub
While (true ){
Try {
Thread. sleep (10000); // Thread pause 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 ();
}
}
}
}
</SPAN>
Public class MyThread implements Runnable {
@ Override
Public void run (){
// TODO Auto-generated method stub
While (true ){
Try {
Thread. sleep (10000); // Thread pause 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:
[Java]
<SPAN style = "COLOR: #993300"> new Thread (new MyThread (). start ();
</SPAN>
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 relatively simple:
[Java]
<SPAN style = "COLOR: #993300"> Handler handler = new Handler ();
Runnable runnable = new Runnable (){
@ Override
Public void run (){
// TODO Auto-generated method stub
// What to do
Handler. postDelayed (this, 2000 );
}
};
</SPAN>
Handler handler = new Handler ();
Runnable runnable = new Runnable (){
@ Override
Public void run (){
// TODO Auto-generated method stub
// What to do
Handler. postDelayed (this, 2000 );
}
};
12. Start the Timer:
[Java]
<SPAN style = "COLOR: #993300"> handler. postDelayed (runnable, 2000); // run runnable every two seconds.
</SPAN>
Handler. postDelayed (runnable, 2000); // run runnable every two seconds.
3. Stop the Timer:
[Java]
<SPAN style = "COLOR: #993300"> handler. removeCallbacks (runnable );
</SPAN>
Handler. removeCallbacks (runnable );
3. Handler is used in combination with timer and TimerTask.
1. Define the timer, timer task, and Handler handle
[Java]
<SPAN style = "COLOR: #993300"> 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 );
}
};
</SPAN>
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.
[Java]
<SPAN style = "COLOR: #993300"> task = new TimerTask (){
@ Override
Public void run (){
// TODO Auto-generated method stub
Message message = new Message ();
Message. what = 1;
Handler. sendMessage (message );
}
};
</SPAN>
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
[Java]
<SPAN style = "COLOR: #993300"> timer. schedule (task, 2000,200 0 );
</SPAN>
Timer. schedule (task, 2000,200 0 );
Briefly describe 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.