This article describes how to use Android handler. Handler can send messsage and runnable objects to the Message Queue of the associated thread. Each handler object is associated with the thread that created it, and each handler object can only be associated with one thread.
Handler generally has two purposes: 1) to execute a scheduled task, you can schedule it to execute some tasks and simulate the timer. 2) Inter-thread communication. When an Android Application is started, a main thread is created, and the main thread creates a message queue to process various messages. When creating a sub-thread, you can get the handler object created in the parent thread in your sub-thread to send messages to the Message Queue of the parent thread. Because Android requires updating the interface in the UI thread, you can update the interface in other threads through this method.
◆ Use runnable to update the interface in the Child thread
○ Create handler in oncreate
Public class handlertestapp extends activity {
Handler mhandler;
Textview mtext;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mhandler = new handler (); // create a handler
Mtext = (textview) findviewbyid (R. Id. text0); // A textview
}
○ Build a runnable object and update the interface in runnable. Here, we modify the text of textview. it must be noted that the runnable object can be created in the main thread or child thread. We created it here in the Child thread.
Runnable mrunnable0 = new runnable ()
{
@ Override
Public void run (){
// Todo auto-generated method stub
Mtext. settext ("this is update from ohter thread, mouse down ");
}
};
○ Create a sub-thread. In the run function of the thread, we send a runnable to the Message Queue of the main thread to update the interface.
Private void updateuibyrunnable (){
New thread ()
{
// Message MSG = mhandler. obtainmessage ();
Public void run ()
{
// Mtext. settext ("this is update from ohter thread, mouse down"); // This statement will throw an exception
Mhandler. Post (mrunnable0 );
}
}. Start ();
}
◆ Use message to update the interface in the Child thread
The message update interface is similar to the runnable update interface, but it only needs to be modified.
○ Implement your own handler to process messages
Private class myhandler extends Handler
{
@ Override
Public void handlemessage (Message MSG ){
// Todo auto-generated method stub
Super. handlemessage (MSG );
Switch (msg. What)
{
Case update: // when receiving a message, update the interface
Mtext. settext ("this update by message ");
Break;
}
}
}
○ Send messages in a new thread
Private void updatebymessage ()
{
// Anonymous object
New thread ()
{
Public void run ()
{
// Mtext. settext ("this is update from ohter thread, mouse down ");
// Update is a defined integer that represents the Message ID.
Message MSG = mhandler. obtainmessage (update );
Mhandler. sendmessage (MSG );
}
}. Start ();
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/yangpeng98/archive/2010/05/26/5625955.aspx