Android provides an asynchronous message callback mechanism Handler, which is used to send messages and process messages and runnable objects in thread message queues.
The Handler instance is used together with a thread and the message queue of the thread. Once a new handler instance is created, the system binds the instance to a message queue of the thread and the thread, which can send messages and runnable objects to the message queue and process them at the Message Queue exit.
Handler uses the current thread RunLoop by default, but it can also use its constructor to specify the runloop of a thread, or use a dedicated HandlerThread.
It must be noted that the Runloop message loop must be implemented in the Handler thread; otherwise, a running exception may occur.
Generally, if Handler is used in the main thread, we do not need to create a runloop automatically because the android system creates a runloop for the main thread of the activity.
Similarly, we can use Handler in a special child thread without creating a runloop. This special sub-thread is HandlerThread. Check its source code. We can find that it also implements runloop in the run method.
The following describes several scenarios and usage of Handler:
1. Use Handler in the main thread
Handler is relatively simple in the main thread. The following sample code creates a Handler object in the main thread and starts a runable method to refresh the progress bar. The Code is as follows:
[Java]
// Create a Handler object in the main thread
// Create a Handler object in the main thread [java] view plaincopyprint? Handler handler = new Handler ()
{
Public void handleMessage (Message msg)
{
FirBar. setProgress (msg. arg1 );
Handler. post (updateThread );
Log. v ("thread ----", Thread. currentThread (). getName ());
}
};
Handler handler = new Handler ()
{
Public void handleMessage (Message msg)
{
FirBar. setProgress (msg. arg1 );
Handler. post (updateThread );
Log. v ("thread ----", Thread. currentThread (). getName ());
}
}; [Java] view plaincopyprint? <SPAN style = "WHITE-SPACE: pre"> handler. post (updateThread); </SPAN> // run
Handler. post (updateThread); // run
[Java]
// Create a runable to refresh the progress bar
// Create a runable to refresh the progress bar [java] view plaincopyprint? Runnable updateThread = new Runnable (){
@ Override
Public void run (){
// TODO Auto-generated method stub
If (I> = 100 ){
Handler. removeCallbacks (updateThread );
Return;
}
I + = 10;
Message msg = handler. obtainMessage ();
Msg. arg1 = I;
Try {
Thread. sleep (1000); // Thread pause for 1 s
} Catch (InterruptedException e ){
// TODO: handle exception
E. printStackTrace ();
}
Handler. sendMessage (msg );
Handler1.sendEmptyMessage (0 );
}
};
Runnable updateThread = new Runnable (){
@ Override
Public void run (){
// TODO Auto-generated method stub
If (I> = 100 ){
Handler. removeCallbacks (updateThread );
Return;
}
I + = 10;
Message msg = handler. obtainMessage ();
Msg. arg1 = I;
Try {
Thread. sleep (1000); // Thread pause for 1 s
} Catch (InterruptedException e ){
// TODO: handle exception
E. printStackTrace ();
}
Handler. sendMessage (msg );
Handler1.sendEmptyMessage (0 );
}
};
2. Use Handler in a normal child thread
The sample code is as follows:
[Java]
// Create a sub-thread in which Handler is used
Class MyThread extends Thread
{
Public void run (){
Logoff. prepare ();
Handler1 = new Handler () // Looper. getMainLooper ()
{
Public void handleMessage (Message msg)
{
Log. v ("SubThread", Thread. currentThread (). getName ());
// The time-consuming operation will not affect the main UI thread.
For (int I = 0; I <1000000000; I ++ ){
}
}
};
Logoff. loop ();
}
}
// Create a sub-thread in which Handler is used
Class MyThread extends Thread
{
Public void run (){
Logoff. prepare ();
Handler1 = new Handler () // Looper. getMainLooper ()
{
Public void handleMessage (Message msg)
{
Log. v ("SubThread", Thread. currentThread (). getName ());
// The time-consuming operation will not affect the main UI thread.
For (int I = 0; I <1000000000; I ++ ){
}
}
};
Logoff. loop ();
}
}
3. Use Handler in the HandlerThread
[Java]
HandlerThread handlerThread = new HandlerThread ("handlerThread ");
HandlerThread. start ();
// Obtain the runloop of HandlerThread.
MyHandler myHandler = new MyHandler (handlerThread. getLooper ());
HandlerThread handlerThread = new HandlerThread ("handlerThread ");
HandlerThread. start ();
// Obtain the runloop of HandlerThread.
MyHandler myHandler = new MyHandler (handlerThread. getLooper ());