Summary
Andriod provides Handler and Looper to meet the communication between threads. For example, a sub-thread downloads a picture from the network and sends a message to the main thread when it is downloaded, and this message is passed by binding to the handler of the main thread.
Body
Graphic:
code example:
/** * @author Allin.dev * http://allin.cnblogs.com */public class Mainthread extends activity{private static F inal String TAG = "Mainthread"; Private Handler Mmainhandler, Mchildhandler; Private TextView info; Private Button msgbtn; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); info = (TextView) Findviewbyid (r.id.info); MSGBTN = (Button) Findviewbyid (R.ID.MSGBTN); Mmainhandler = new Handler () {@Override public void Handlemessage (Message msg) {LOG.I ( TAG, "Got an incoming message from the child thread-" + (String) msg.obj); Receives the message Info.settext (String) msg.obj) of the child thread; } }; New Childthread (). Start (); Msgbtn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { if (Mchildhandler! = null) {//Send message to child thread message childmsg = Mchildhandler.obtainmessage (); Childmsg.obj = Mmainhandler.getlooper (). GetThread (). GetName () + "says Hello"; Mchildhandler.sendmessage (CHILDMSG); LOG.I (TAG, "Send a message to the child thread-" + (String) childmsg.obj); } } } ); } public void OnDestroy () {Super.ondestroy (); LOG.I (TAG, "Stop looping the child thread ' s Message queue"); Mchildhandler.getlooper (). Quit (); } class Childthread extends Thread {private static final String Child_tag = "Childthread"; public void Run () {this.setname ("Childthread"); Initializes the message loop queue, which needs to be looper.prepare () before handler is created; Mchildhandler = new Handler () {@Override public void Handlemessage (Message msg) {L OG.I (Child_tag, "Got a incoming message from the main thread-" + (String) msg.obj); try {//can do some time-consuming work in a child thread sleep (100); Message Tomain = Mmainhandler.obtainmessage (); Tomain.obj = "This is" + this.getlooper (). GetThread (). GetName () + ". Did you send me \ "" + (String) msg.obj + "\"? "; Mmainhandler.sendmessage (Tomain); LOG.I (Child_tag, "Send a message to the main thread-" + (String) tomain.obj); } catch (Interruptedexception e) {//TODO auto-generated catch block E.printsta Cktrace (); } } }; LOG.I (Child_tag, "child handler was bound to-" + mchildhandler.getlooper (). GetThread (). GetName ()); Initiates a child thread message loop queue Looper.loop (); } }}
Ps:
Using the Handlerthread Looper object to create the handler, if you use the default construction method, it is likely to block the UI thread, referring to http://www.cnblogs.com/ccdc/p/3837798.html. Improvement scheme A new thread will be used to replace the handler of the main course with the looper of the main thread looper, as below,
Reference
Shangdawei.android inter-thread communication [2014-07-11] (2013-03-26). http://www.cnblogs.com/shangdawei/archive/2013/03/26/2983217.html
Http://www.cnblogs.com/ccdc/p/3837883.html
Android Thread Communication