Android sends local broadcasts and creates broadcast receivers

Source: Internet
Author: User

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

Related Article

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.