This article mainly introduces how to use Android handler. Handler can send messsage and runnable objects to the message queue of the thread associated with them. Each handler object is associated with the line threads that created it, and each handler object can be associated with only one line threads.
- Handler generally have two purposes: 1) Perform a scheduled task, you can perform certain tasks on the scheduled implementation, and you can simulate the timer. 2) Inter-thread communication. When Android apps start, a main thread is created, and the main thread creates a message queue to handle 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 that object. Because Android requires the interface to be updated in the UI thread, you can update the interface in other threads with this method.
Example of updating the interface through runnable in a sub-thread
- 0 Create handler
- public class Handlertestapp extends Activity {
    &N in OnCreate bsp; Handler Mhandler;
TextView MText;
/** Called when the activity is first created. */
& nbsp; @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
}
0 Build the Runnable object and update the interface in Runnable, where we have modified the TextView text. It is necessary to note that the Runnable object can be created again in the main thread or in a sub-thread. We created this here in a child thread.
Runnable MRunnable0 = new Runnable ()
{
@Override
public void Run () {
TODO auto-generated Method Stub
Mtext.settext ("This was Update from Ohter thread, Mouse down");
}
};
0 Create a sub-thread, in the thread's run function, we send a runnable to the main thread's message queue to update the interface.private void updateuibyrunnable () {
New Thread ()
{
Message msg = Mhandler.obtainmessage ();
public void Run ()
{
Mtext.settext ("This was Update from Ohter thread, Mouse down");//This sentence will throw an exception
Mhandler.post (MRUNNABLE0);
}
}.start ();
}
Update the interface with a message in a child thread
Using the message update interface is similar to the Runnable update interface, except that there are several places to modify.
0 implement your own handler and process the message
Private class MyHandler extends Handler
{
@Override
public void Handlemessage (Message msg) {
//TODO auto-generated Method Stub
Super.handlemessage (msg);
switch (msg.what)
{
Case update://updates the interface when the message is received
Mtext.settext ("This update by message");
break;
}
}
}
0 Send a message in a new thread
private void Updatebymessage ()
{
& nbsp Anonymous object
new Thread ()
{
public void Run ()
{
//mtext.settext ("This was Update from Ohter thread, Mouse down");
//update is a self-defined integer that represents the message ID
Message msg = mhandler.obtainmessage (UPDATE);
mhandler.sendmessage (msg);
}
}.start ();
}