1. Create a local broadcast receiver
- Public classMainactivityextendsAppcompatactivity{ + PrivateButton mBtn; + PrivateBindservice.mybinder Mmybinder; A PrivateMyreceiver Mmyreceiver;//Create a broadcast receiver class variable at PrivateLocalbroadcastmanager Mlocalbroadcastmanager;//To create a local broadcast manager class variable - - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -Mmyreceiver =NewMyreceiver ();//instantiate a broadcast receiver Myreceiver () $Intentfilter filter =NewIntentfilter ("Com.package.yanglei.action.test");//dynamically configuring the broadcast sink action $Mlocalbroadcastmanager = Localbroadcastmanager.getinstance ( This);//broadcast variable manager get local broadcast manager -Mlocalbroadcastmanager.registerreceiver (mmyreceiver, filter);//Register for local broadcast - the -MBtn =Findviewbyid (R.id.monclick);WuyiMbtn.setonclicklistener (NewView.onclicklistener () { the @Override - Public voidOnClick (view view) { Wu -Intent Intent =NewIntent ("Com.package.yanglei.action.test");//create an action to send a broadcast AboutIntent.putextra (Intent.extra_text, "Hi lllllllllll");//Send the data you carry $Mlocalbroadcastmanager.sendbroadcast (Intent);//send a local broadcast + } the }); $ the } the theBroadcast receivers the Private classMyreceiverextendsBroadcastreceiver { - @Override in Public voidOnReceive (Context context, Intent Intent) { theLOG.I ("Hhhhhhh0", Intent.getstringextra (Intent.extra_text)); Get text the } About }}
Android sends local broadcasts and creates broadcast receivers