Android Note (32) communication between Android threads (four) The main thread sends a message to a child thread

Source: Internet
Author: User

The previous example is that we process and send messages in the child thread (workerthread), then get the message in the main thread (UI thread) and modify the UI, so we can not send the message from the main threads, the child thread receives it? Let's write the code according to the previous idea:

 Packagecn.lixyz.handlertest;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {Privatebutton button; PrivateHandler Handler; Private inti = 0; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.id.button); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {LOG.D ("Tttt", "OnClick:" +Thread.CurrentThread (). GetName ()); Message msg=Handler.obtainmessage ();            Handler.sendmessage (msg);        }        }); Workerthread WT=NewWorkerthread ();    Wt.start (); }    classWorkerthreadextendsThread {@Override Public voidrun () {Super. Run (); Handler=NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); LOG.D ("Tttt", "Message Object Received");        }            }; }    }}
Mainactivity.java
<LinearLayoutxmlns: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"android:orientation= "vertical"Tools:context=". Mainactivity ">    <ButtonAndroid:id= "@+id/button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"Android:layout_margintop= "30DP"Android:text= "Send Message" /></LinearLayout>
Activity_main.xml

The above problem seems to be nothing wrong, but the runtime is wrong

Throw exception:

09-17 04:01:31.655  15854-15869/? E/androidruntime:fatal exception:thread-120    Process:cn.lixyz.handlertest, pid:15854    Java.lang.RuntimeException:Can ' t create handler inside thread that have not called looper.prepare () at            Android.os.Han Dler.<init> (handler.java:200) at            android.os.handler.<init> (handler.java:114)            at Cn.lixyz.handlertest.mainactivity$workerthread$1.<init> (mainactivity.java:48) at            Cn.lixyz.handlertest.mainactivity$workerthread.run (mainactivity.java:48)

  

Tip We did not call Looper.prepare () and could not create the thread because the Looper object is not created by default in the main thread and needs to be called looper.prepare () to enable Looper first. Modify the above code.

 Packagecn.lixyz.handlertest;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Looper;ImportAndroid.os.Message;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {Privatebutton button; PrivateHandler Handler; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.id.button); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Message msg=Handler.obtainmessage (); Msg.what= 100;                Handler.sendmessage (msg); LOG.D ("Tttt", "SendMessage:" +Thread.CurrentThread (). GetName ());        }        }); Workerthread WT=NewWorkerthread ();    Wt.start (); }    classWorkerthreadextendsThread {@Override Public voidrun () {Super. Run ();            Looper.prepare (); Handler=NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); LOG.D ("Tttt", "Handlemessage:" +Thread.CurrentThread (). GetName ()); inti =Msg.what; LOG.D ("Tttt", "Received Message object:" +i);            }            };        Looper.loop (); }    }}
Mainactivity.java
<LinearLayoutxmlns: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"android:orientation= "vertical"Tools:context=". Mainactivity ">    <ButtonAndroid:id= "@+id/button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"Android:layout_margintop= "30DP"Android:text= "Send Message" /></LinearLayout>
Activity_main.xml

Operation Result:

09-17 04:29:48.669  28397-28397/cn.lixyz.handlertest d/TTTT:sendMessage:main09-17 04:29:48.669  28397-28410/cn.lixyz.handlertest d/tttt:handlemessage:thread-14109-17 04:29:48.669  28397-28410/ Cn.lixyz.handlertest D/tttt: received the Message object: 100

The above code indicates that the message was sent from Mainthread, and Workerthread received the message.

PS: The non-UI thread defaults to the creation of the Looper object, so when the mainline Cheng thread is required to deliver the message, the Looper object is created and the loop is started, and the child thread can loop the message out of the maessagequeue.   

Android Note (32) communication between Android threads (four) The main thread sends a message to a 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.