Recently, I have found some information about Android thread communication, sorted it out, and made a simple example.
Andriod provides handler and logoff to satisfy inter-thread communication. For example, if a sub-thread downloads an image from the network and sends a message to the main thread after the download is completed, the message is transmitted by handler bound to the main thread.
In Android, threads here are divided into threads with message loops and threads without message loops. Threads with message loops generally have a logoff, a new concept of Android. Our main thread (ui thread) is a message loop thread. To address this message loop mechanism, we introduce a new mechanism handle. If we have a message loop, we need to send corresponding messages to the message loop, custom messages generally have their own processing, sending and clearing of messages, and processing of messages. These are encapsulated in handle. Note that handle is only for those threads with logoff, whether it's a UI thread or a sub-thread, as long as you have logoff, I can add something to your message queue and handle it accordingly. However, there is another point here, that is, as long as it is about the UI, it cannot be placed in the Child thread, because the child thread cannot operate the UI, only data, system, and other non-UI operations can be performed.
In Android, threads are divided into threads with message loops and threads without message loops. Threads with message loops generally have a logoff, which is a new concept of Android. Our main thread (ui thread) is a message loop thread. For this message loop mechanism, we introduce a new mechanism handler. If we have a message loop, we need to send the corresponding message to the message loop, custom messages generally have their own processing, send and clear messages, and encapsulate these in handler. Note that handler only targets those threads with logoff, whether it's a UI thread or a sub-thread, as long as you have logoff, I can add something to your message queue and handle it accordingly.
However, there is another point here, that is, as long as it is about the UI, it cannot be placed in the Child thread, because the child thread cannot operate the UI, only data, system, and other non-UI operations can be performed.
The creation of a handler will be bound to the Message Queue of this thread. If it is created in the main thread, you do not need to write code to create the message queue, the default message queue is created in the main thread. However, if it is in a subthread, the thread message queue must be initialized before handler is created. The following code:
Class childthread extends thread {public void run () {/** initialize logoff before creating a handler. */logoff. prepare ();/** create handler in the subthread, so it will be bound to the subthread's Message Queue **/mchildhandler = new handler () {public void handlemessage (Message MSG) {/** do some expensive operations there. */};/** start the message queue of this thread */logoff. loop ();}}
When handler receives the message, it will run handlemessage (...) You can perform some time-consuming operations in the callback function.
When the operation is completed to end the sub-thread, remember to call quit () to end the message loop queue.
mChildHandler.getLooper().quit();
The following is a small example of inter-thread communication:
/***** @ Author allin. dev * http://allin.cnblogs.com **/public class mainthread extends activity {Private Static final string tag = "mainthread"; private handler mmainhandler, mchildhandler; private textview Info; private button msgbtn; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Info = (textview) findviewbyid (r.id.info); MS Gbtn = (button) findviewbyid (R. id. msgbtn); mmainhandler = new handler () {@ overridepublic void handlemessage (Message MSG) {log. I (TAG, "got an incoming message from the Child thread-" + (string) MSG. OBJ); // receives the message info of the subthread. settext (string) MSG. OBJ) ;}}; new childthread (). start (); msgbtn. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {If (mchildhandler! = NULL) {// send the message to the subthread 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"); // initialize the message loop queue, which must be loved before handler is created. prepare (); mchildhandler = new handler () {@ overridepublic void handlemessage (Message MSG) {log. I (child_tag, "got an incoming message from the main thread-" + (string) MSG. OBJ); try {// some time-consuming work can be done in the Child thread sleep (100); message tomain = mMain Handler. 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 blocke. printstacktrace () ;}}; log. I (child_tag, "Child handler is bound to-" + mchildhandler. getlooper (). getthread (). getname (); // subscriber thread message loop queue logoff. loop ();}}}
[Download source code]