Each handler object is associated with the line threads relative that created it, and each handler object can only be associated with one line threads relative.
Handler is generally used for two purposes: 1 execution of Scheduled tasks, you can be scheduled to perform certain tasks, you can simulate the timer. 2) communication between threads. When the Android application starts, a main thread is created, and the main thread creates a message queue to handle the various messages. When you create a child thread, you can then get the handler object created in the parent thread from your child thread, and you can send a message to the parent thread's message queue through the object. Because Android requires updating the interface in the UI thread, you can update the interface in other threads by this method.
An example of updating the interface through runnable in a child thread
Creating Handler in OnCreate
Copy Code code as follows:
public class Handlertestapp extends activity {
Handler Mhandler;
TextView Mtext;
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Mhandler = new Handler ();//Create Handler
Mtext = (TextView) Findviewbyid (r.id.text0);//A TextView
}
Build the Runnable object and update the interface in Runnable, where we have modified the TextView text. What you need to note here is that the Runnable object can be created again in the main thread or in a child thread. We created this here in a child thread.
Copy Code code as follows:
Runnablemrunnable0=newrunnable ()
{
@Override
Publicvoidrun () {
Todoauto-generatedmethodstub
Mtext.settext ("Thisisupdatefromohterthread,mousedown");
}
};
To create a child thread, in the run function of the thread, we send a runnable to the main thread's message queue to update the interface.
Copy Code code as follows:
Privatevoidupdateuibyrunnable () {
Newthread ()
{
Messagemsg=mhandler.obtainmessage ();
Publicvoidrun ()
{
Mtext.settext ("Thisisupdatefromohterthread,mousedown");/This sentence throws an exception
Mhandler.post (MRUNNABLE0);
}
}.start ();
}
Update the interface with message in child threads
Updating the interface with the message is similar to the Runnable update interface, but it needs to be modified in several places.
Implement your own handler and process the message
Copy Code code as follows:
Privateclassmyhandlerextendshandler
{
@Override
Publicvoidhandlemessage (messagemsg) {
Todoauto-generatedmethodstub
Super.handlemessage (msg);
Switch (msg.what)
{
caseupdate://updates the interface when a message is received
Mtext.settext ("Thisupdatebymessage");
Break
}
}
}
Send a message in a new thread
Copy Code code as follows:
Privatevoidupdatebymessage ()
{
Anonymous objects
Newthread ()
{
Publicvoidrun ()
{
Mtext.settext ("Thisisupdatefromohterthread,mousedown");
Update is a defined integer that represents the message ID
Messagemsg=mhandler.obtainmessage (UPDATE);
Mhandler.sendmessage (msg);
}
}.start ();
}