Using broadcast for communication between Android components

Source: Internet
Author: User
<span id="Label3"></p><p><p>There are many ways to implement communication between Android components, and broadcast is one of Them. Communication between activity and fragment, broadcast use more This article takes an activity as an example.<br>Effect<br></p></p><p><p>Layout file:</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">linearlayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute"> Xmlns:tools</span>=<span class="hljs-value">"http://schemas.android.com/tools"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent" </span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span><span class="hljs-attribute">android:orientation</span>=<span class="hljs-value">"vertical" </span>> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">textview </span> <span class="hljs-attribute">android:id </span> =<span class="hljs-value"> "@+id/textview1" </span> <span class="hljs-attribute">android:layout_width </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:layout_height </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:text </span> =<span class="hljs-value"> "@string/hello_world" </span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">button</span><span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/button1"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"wrap_ Content "</span><span class="hljs-attribute">android:layout_height</span>="<span class="hljs-value">wrap_content "</span><span class="hljs-attribute">android:layout_alignleft</span>=<span class="hljs-value">" @+id /textview1 "</span><span class="hljs-attribute">android:layout_marginleft</span>=<span class="hljs-value">" 27DP "</span><span class="hljs-attribute">android:layout_margintop</span>= <span class="hljs-value">"26dp"</span> <span class="hljs-attribute">android:text</span>=<span class="hljs-value">"send broadcast"</span> /> </span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">linearlayout</span>></span></span></code></pre><p><p>Mainactivity.java</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Button btn;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>TextView tv;<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_main); TV = (TextView)<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. Findviewbyid (r.id.textview1);<span class="hljs-comment"><span class="hljs-comment">//receive broadcasts</span></span>Localbroadcastmanager Broadcastmanager = localbroadcastmanager. getinstance (mainactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); Intentfilter Intentfilter =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intentfilter (); Intentfilter.addaction (<span class="hljs-string"><span class="hljs-string">"com.example.test1"</span></span>); Broadcastreceiver Mitemviewlistclickreceiver =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Broadcastreceiver () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnReceive</span></span>(context context, Intent Intent) {tv.settext (<span class="hljs-string"><span class="hljs-string">"1111"</span></span>); } }; Broadcastmanager.registerreceiver (mitemviewlistclickreceiver, intentfilter); BTN = (Button)<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. Findviewbyid (r.id.button1); Btn.setonclicklistener (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Onclicklistener () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnClick</span></span>(View V) {<span class="hljs-comment"><span class="hljs-comment">//send Broadcast</span></span>Intent Intent =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent (<span class="hljs-string"><span class="hljs-string">"com.example.test1"</span></span>); Localbroadcastmanager.getinstance (mainactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>). Sendbroadcast (intent); } }); }}</code></pre> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Using broadcast for communication between Android components</p></p></span>

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.