This article mainly introduces the use of 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 a line threads that creates it. And each handler object can only be associated with a single line threads.
- Handler generally have two purposes: 1) Run a scheduled task, and you can run certain tasks on the scheduled implementation. Ability to simulate timers. 2) Inter-thread communication. When the Android app starts. A main thread is created, and the main thread creates a message queue to process 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 that object. Because Android requires the interface to be updated in the UI thread, this method allows the interface to be updated in other threads.
Example of updating an interface in a sub-thread by runnable
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 a Runnable object. Update the interface in Runnable, where we have changed the text of TextView. It is necessary to note that the Runnable object can be created again in the main thread. Can also be created 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"); } };
creates 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, only a few changes are needed.
To achieve their own handler. Processing 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 custom integer that represents the message ID of msg = Mhandler.obtainmessage (UPDATE); Mhandler.sendmessage (msg); } }. Start (); }
Android--handler usage: Updating the interface in a sub-thread