Android Learning notes broadcast intent and broadcast recipient Mybroadcastreceiver, broadcast

Source: Internet
Author: User

(1) First to register with an XML file

Layout file, add a button click on the time to broadcast

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=". Mainactivity ">    <button        android:id=" @+id/button1 "        android:layout_width=" Wrap_content        " android:layout_height= "Wrap_content"        android:layout_alignparentleft= "true"        android:layout_ Alignparenttop= "true"        android:layout_marginleft= "68DP"        android:layout_margintop= "82DP"        android: text= "button"/></relativelayout>

Mainactivity.java

Package Com.lc.broadcastreceiver_demo;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.menu;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Com.example.broadcastreceiver_demo. r;/* * The broadcast recipient registered with the XML file needs to register the * <receiver android:name= "Com.lc.broadcastreceiver_demo in the manifest file. Mybroadcastreceiver "> <intent-filter> <action android:name=" Com.freedie.broadcast " /> </intent-filter> </receiver> */public class Mainactivity extends Activity {private but ton button, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main), Button = (button) This.findviewbyid (r.id.button1),/* * * When the button is clicked */ Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {if (R.id.button1 = = V.getid ()) { Intent Intent = new Intent (); Intent.setaction ("CoM.freedie.broadcast "); Sendbroadcast (intent); Send broadcast Intent}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

Mybroadcastreceiver.java the recipient of the broadcast, to inherit the Broadcastreceiver class, and to overload the OnReceive method:

Package Com.lc.broadcastreceiver_demo;import Android.content.broadcastreceiver;import Android.content.Context; Import Android.content.intent;import android.util.log;/* * Broadcast recipient, the OnReceive callback method executes */public class when the broadcast recipient receives the broadcast intent Mybroadcastreceiver extends Broadcastreceiver {private static final String TAG = "Mybroadcastreceiver"; @Overridepublic void OnReceive (Context context, Intent Intent) {if ("Com.freedie.broadcast". Equals (Intent.getaction ())) {LOG.D (TAG, " Com.freedie.broadcast Intent was receiverd! ");}}}


To register a broadcast in the manifest file:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.broadcastreceiver_demo "android:versioncode=" 1 "android:versionname=" 1.0 "> & LT;USES-SDK android:minsdkversion= "8" android:targetsdkversion= "/> <application android: Allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" Android:them E= "@style/apptheme" > <activity android:name= "Com.lc.broadcastreceiver_demo. Mainactivity "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER        "/> </intent-filter> </activity> <!--registration of Mybroadcastreceiver <receiver android:name= "Com.lc.broadcastreceiver_demo. MyBRoadcastreceiver "> <intent-filter> <action android:name=" Com.freedie.broadcast "/&G            T </intent-filter> </receiver> </application></manifest>


(2) The second use of Java code to register

The layout file does not change (contains a button when clicked to broadcast), the manifest file is not allowed to operate, the Mybroadcastreceiver.java file does not change, only applies to test whether receive success! :

Mainactivity.java

Package Com.example.broadcastreceiver_javacode;import Android.os.bundle;import Android.app.activity;import Android.content.intent;import Android.content.intentfilter;import Android.view.menu;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity { Private Button button;private mybroadcastreceiver mybroadcastreceiver, @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); button = (button) This.findviewbyid (R.id.button1); Button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View V) {if (R.id.button1 = = V.getid ()) {Intent Intent = new Intent (); Intent.setaction ("Com.freedie.brodcast"); sendbroadcast (intent); Send broadcast}});} /* Register a broadcast program here */@Overrideprotected void Onresume () {super.onresume (); mybroadcastreceiver = new Mybroadcastreceiver () ;//Intent filter Intentfilter filter = new Intentfilter (); Filter.addaction ("com. Freedie.brodcast ");//Register the broadcast recipient Registerreceiver (Mybroadcastreceiver, filter);} /* * Write off the broadcast recipient */@Overrideprotected void OnStop () {super.onstop (); Unregisterreceiver (mybroadcastreceiver);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}



Android Learning notes broadcast intent and broadcast recipient Mybroadcastreceiver, broadcast

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.