Mainactivity such as the following:
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: * * Demo sample process such as the following: * 1 child threads to the child thread itself Send Message * 2 after receiving 1 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 to itself to send the message, the key is to construct handler when the incoming looper. * In this case, the child thread's own Looper is called Looper.mylooper (), code such as the following: * 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 point only to pay attention to the construction * handler when the incoming handler is the main thread handler, that is, 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, there is nothing to send messages between these threads, and the key is to pass in the Looper when constructing the handler. * */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 = "Mainline ChengMessage sent by Xi~xi "; mhandlertest1.sendmessage (message); counter++;}}}
Main.xml such as the following:
<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 specific explanation series (iv)--using handler to send messages between the main thread and the child thread