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