Handler detailed series (iv)--using handler to send messages between the main thread and the child thread

Source: Internet
Author: User
Tags message queue

Mainactivity as follows:

Package Cc.c;import Android.app.activity;import Android.os.bundle;import android.os.handler;import Android.os.looper;import Android.os.message;import android.widget.textview;/** * Demo Description: * * Example steps are as follows: * 1 child threads send messages to the child thread itself * 2 after receiving 1 of the message, the child thread sends a message to the main thread * 3 receives a 2 message, the main thread sends a message to the child thread * * To implement the child thread itself to send messages to itself, the key is to construct handler when the Looper passed in. * This will be passed to the child thread's own Looper called Looper.mylooper (), the code is as follows: * Looper.prepare (); * Mhandlertest1=new HandlerTest1 (Looper.mylooper ()); * Looper.loop (); * * So when mhandlertest1.sendmessage (message) is sent, it is, of course, sent to its own message queue. * * When you receive a message from a child thread, you can continue to send the message to the main thread. At this time as long as the attention constructs * Handler when the incoming handler is the main thread handler, namely Getmainlooper (). * There's nothing else to say. * * * * After the main thread processes the message and then sends the message to the child thread * * * In fact, these threads send messages, there is nothing, the key is to construct handler when the Looper to pass in. * */public class Mainactivity extends Activity {private TextView mtextview;private HandlerTest1 mhandlertest1;private Han DlerTest2 mhandlertest2;private int counter=0; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.main); inIt ();} private void Init () {Mtextview = (TextView) Findviewbyid (r.id.textview);//1 child thread sends a message to itself new thread () {public void run () {Loo Per.prepare (); Mhandlertest1=new HandlerTest1 (Looper.mylooper ()); Message message = new Message (); message.obj = "message Hi~hi sent by the child thread"; mhandlertest1.sendmessage (message); Looper.loop ();};}. Start ();} Private class HandlerTest1 extends Handler {private HandlerTest1 (Looper Looper) {super (Looper);} @Overridepublic void Handlemessage (Message msg) {super.handlemessage (msg); System.out.println ("Sub-thread Received:" + msg.obj);//2 can send messages to the main thread mhandlertest2=new HandlerTest2 (Getmainlooper ()) after receiving the message; Message message = new Message (); message.obj = "O (∩_∩) o"; mhandlertest2.sendmessage (message);}} Private class HandlerTest2 extends Handler {private HandlerTest2 (Looper Looper) {super (Looper);} @Overridepublic void Handlemessage (Message msg) {super.handlemessage (msg); Mtextview.settext ("In the main thread, receive a message from a child thread:" + msg.obj);//3 receives the message and then sends the message to the child thread if (counter==0) {Message message = new Message (); message.obj = "message Xi~xi sent by the main thread";Mhandlertest1.sendmessage (message); counter++;}}} 

Main.xml as follows:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    >    <textview        android:id= "@+id/textview"        android:layout_width= "Wrap_content"        android: layout_height= "Wrap_content"        android:text= "@string/hello_world"        android:layout_centerinparent= "true"        android:layout_margintop= "70dip"/>    </RelativeLayout>



Handler detailed series (iv)--using handler to send messages between the main thread and the child thread

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.