Android thread Communication

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.