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
Create Handlerpublic class Handlertestapp extends Activity { Handler mhandler in OnCreate; 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 Handler MText = (TextView) Findviewbyid (r.id.text0);//A TextView }
constructs the Runnable object, updates the interface in the runnable, here, 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"); } };
Creating a child 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 is Update from Ohter thread, Mouse down");//This sentence throws 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.
Implement your own Handler, 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 it receives the message mtext.settext ("This update by message"); Break;}}} Send message in new thread private void Updatebymessage () { ///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, msg = Mhandler.obtainmessage (UPDATE); Mhandler.sendmessage (msg); } }. Start (); }
How to use Android--handler: Update the interface in a sub-thread