Communication between broadcast and activity in Android

Source: Internet
Author: User

Before reading this article, if you are not familiar with the Android broadcasting mechanism, it is recommended to read one of my blog posts: graphic Android broadcast mechanism.

Because this case is relatively simple, so directly posted in the code, do not do too much elaboration.

First on the effect:

The code for Mainactivity is as follows:
Package Com.gc.testbroadcasedemo;import Android.app.activity;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.intentfilter;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; Import android.widget.textview;/** * * @author Android General * */public Class Mainactivity extends Activity {private Button M Button;private TextView mtextview;public static String action_intent_test = "Com.gc.broadcase.test"; overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main) Mtextview = (TextView) Findviewbyid (R.ID.MESSAGE_TV); Mbutton = (Button) Findviewbyid ( R.ID.SEND_BTN); Mbutton.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method Stubintent mintent = new Intent (action_intent_test); Sendbroadcast (mintent);}}); Registermessagereceiver ();}@overrideprotected void OnDestroy () {unregisterreceiver (mmessagereceiver), Super.ondestroy () to be unbound with broadcast when destroying; Public messagereceiver mmessagereceiver;public static String action_intent_receiver = "Com.gc.broadcast.receiver";/** * Dynamic registration broadcast */public void Registermessagereceiver () {mmessagereceiver = new messagereceiver (); intentfilter filter = new Inten Tfilter (); filter.addaction (action_intent_receiver); Registerreceiver (mmessagereceiver, filter);} public class Messagereceiver extends Broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {/ /TODO auto-generated Method Stubif (Intent.getaction (). Equals (Action_intent_receiver)) {Mtextview.settext ( Intent.getstringextra ("message"));}}}

The code for Mybroadcast is as follows:
Package Com.gc.testbroadcasedemo;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import android.os.bundle;import android.util.log;/** *  * @author Android General * */public class Mybroadcast extends Broadcastreceiver{public mybroadcast () {log.v ("Broadcast_tag", "Mybroadcast");} @Overridepublic void OnReceive (context context, Intent Intent) {//TODO auto-generated method stublog.v ("Broadcast_tag", "OnReceive"); Bundle bundle = Intent.getextras (); if (Intent.getaction (). Equals (Mainactivity.action_intent_test)) { Processcustommessage (context, bundle);}} Send msg to mainactivityprivate void Processcustommessage (context context, bundle bundle) {Intent mintent=new Intent (Ma Inactivity.action_intent_receiver); Mintent.putextra ("Message", "test communication between broadcast and activity"); Context.sendbroadcast (mintent);}}
The case engineering catalogue is as follows:

To register Mybroadcast broadcasts statically in Androidmanifest, the code is as follows:
<receiver android:name= ". Mybroadcast ">            <intent-filter >                <action android:name=" com.gc.broadcase.test "/>            </ Intent-filter>        </receiver>
Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/39928243

Communication between broadcast and activity in Android

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.