Reference: http://codinghard.wordpress.com/2009/05/16/android-thread-messaging/
Download link
Source code:
Package Sample. thread. messaging; Import Android. App. activity; Import Android. OS. Bundle; Import Android. OS. Handler; Import Android. OS. logoff; Import Android. OS. message; Import Android. util. log; Import Android. View. view; Import Android. View. View. onclicklistener; Import Android. widget. Button; Import Android. widget. textview; /** * @ Author David Kuo * */ Public Class Threadmessaging Extends Activity { Private Static Final String tag = "threadmessaging" ; Private Textview mtextview; Private Handler mmainhandler, mchildhandler; /** Called when the activity is first created. */ @ Override Public Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. Main); mtextview = (Textview) findviewbyid (R. Id. Text ); /* * Create the main handler on the main thread so it is bound to the main * thread's message queue. */ Mmainhandler = New Handler (){ Public Void Handlemessage (Message MSG) {log. I (tag, "Got an incoming message from the Child thread-" + (String) msg. OBJ ); /* * Handle the message coming from the Child thread. */ Mtextview. settext (mtextview. gettext () + (String) msg. OBJ + "\ n" );}}; /* * Start the child thread. */ New Childthread (). Start (); log. I (tag, "Main handler is bound to-" + Mmainhandler. getlooper (). getthread (). getname (); button = (Button) findviewbyid (R. Id. Button); button. setonclicklistener ( New Onclicklistener () {@ override Public Void Onclick (view v ){ /* * We cannot guarantee that the mchildhandler is created * in the Child thread by the time the user clicks the button. */ If (Mchildhandler! = Null ){ /* * Send a message to the Child thread. */ Message msg =Mchildhandler. obtainmessage (); msg. OBJ = Mmainhandler. getlooper (). getthread (). getname () + "says hello" ; Mchildhandler. sendmessage (MSG); log. I (tag, "Send a message to the Child thread-" + (String) msg. OBJ) ;}}) ;}@ override Protected Void Ondestroy () {log. I (tag, "Stop looping the child thread's message queue" ); /* * Remember to stop the Logoff */ Mchildhandler. getlooper (). Quit (); Super . Ondestroy ();} Class Childthread Extends Thread { Private Static Final String inner_tag = "childthread" ; Public Void Run (){ This . Setname ("child" ); /* * You have to prepare the loginbefore creating the handler. */ Logoff. Prepare (); /* * Create the child handler on the Child thread so it is bound to the * child thread's message queue. */ Mchildhandler = New Handler (){ Public Void Handlemessage (Message MSG) {log. I (inner_tag, "Got an incoming message from the main thread-" + (String) msg. OBJ ); /* * Do some expensive operation there. For example, you need * to constantly send some data to the server. */ Try { /* * Mocking an expensive operation. It takes 100 MS to * complete. */ Sleep ( 100 ); /* * Send the processing result back to the main thread. */ Message tomain = Mmainhandler. obtainmessage (); tomain. OBJ = "This is" + This . Getlooper (). getthread (). getname () + ". Did you send me \" "+ (string) msg. OBJ + "\"? " ; Mmainhandler. sendmessage (tomain); log. I (inner_tag, "Send a message to the main thread-" + (String) tomain. OBJ );} Catch (Interruptedexception e ){ // Todo auto-generated Catch Block E. printstacktrace () ;}}; log. I (inner_tag, "Child handler is bound to-" + Mchildhandler. getlooper (). getthread (). getname ()); /* * Start looping the message queue of this thread. */ Logoff. Loop ();}}}
End