Android Thread Communication

Source: Internet
Author: User
Tags message queue

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

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.