Explanation of the broadcast Receiver component in Android

Source: Internet
Author: User

Broadcastreceiver (broadcast receiver) is one of the four main components in Android.

 

The following is an overview of broadcastreceiver in Android DOC:
① A broadcast receiver is a component that focuses on receiving and processing broadcast notifications. Many broadcasts originate from system code, such as notifying time zone changes, low battery, taking a picture, or changing language options. Applications can also be broadcast-for example, to notify other applications that some data has been downloaded and is available.
② An application can have any number of broadcast receivers to respond to all notifications that interest it. All receivers inherit from the broadcastreceiver base class.
③ The broadcast receiver has no user interface. However, they can start an activity to respond to the information they receive, or use icationicationmanager to notify users. Notifications can be used in many ways to attract users' attention-flashing back lights, vibrations, and playing sounds. In general, a persistent icon is placed on the status bar. You can open it and get the message.

 

There are two types of broadcast events in Android: System broadcast events, such as action_boot_completed (triggered after the system starts) and action_time_changed (triggered when the system time changes ), action_battery_low (triggered when the power is low) and so on. Another is our custom broadcast event.

 

Broadcast event Process
① Register broadcast events: either static registration or androidmanifest. as defined in the XML file, the registered broadcast receiver must inherit the broadcastreceiver; the other is dynamic registration, which is to use context in the program. the registered broadcast receiver is equivalent to an anonymous class. Both methods require intentfilter.
② Send broadcast events: Send events through context. sendbroadcast, and intent transmits the actions used for registration.
③ Receive broadcast events: When the sent broadcast is listened to by the receiver, it will call its onreceive () method and send the intent object containing the message to it. The code execution time in onreceive should not exceed 5 S; otherwise, the android system will pop up a time-out dialog.

 

The following code demonstrates the use of custom broadcast events and System broadcast events. Complete code:Android_broadcastreceiver.rar

Step 1: register the broadcast event in the onstart method of mainactivity. Static registration is performed in the androidmanifest. xml file.

Step 2: Click the corresponding button to trigger the corresponding method to send the broadcast message.

/** <Br/> * mainactivity <br/> * @ author zuolongsnail IL <br/> */<br/> public class mainactivity extends activity {<br /> private button sendstaticbtn; <br/> private button senddynamicbtn; <br/> private button sendsystembtn; <br/> Private Static final string staticaction = "com. byread. static "; <br/> Private Static final string dynamicaction =" com. byread. dynamic "; <br/> // USB device connection <br/> private stati C Final string systemaction = intent. action_power_connected; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> sendstaticbtn = (button) findviewbyid (R. id. send_static); <br/> senddynamicbtn = (button) findviewbyid (R. id. send_dynamic); <br/> sendsystembtn = (button) findviewbyid (R. id. send_system); <br/> Sendstaticbtn. setonclicklistener (New myonclicklistener (); <br/> senddynamicbtn. setonclicklistener (New myonclicklistener (); <br/> sendsystembtn. setonclicklistener (New myonclicklistener (); <br/>}< br/> class myonclicklistener implements onclicklistener {<br/> @ override <br/> Public void onclick (view V) {<br/> // send custom static registration broadcast messages <br/> If (v. GETID () = R. id. send_static) {<br/> log. E ("mainactivity", "Send custom static Register broadcast message "); <br/> intent = new intent (); <br/> intent. setaction (staticaction); <br/> intent. putextra ("MSG", "received static registration broadcast successful! "); <Br/> sendbroadcast (intent); <br/>}< br/> // send custom Dynamic Registration broadcast message <br/> else if (v. GETID () = R. id. send_dynamic) {<br/> log. E ("mainactivity", "Send custom dynamic register broadcast message"); <br/> intent = new intent (); <br/> intent. setaction (dynamicaction); <br/> intent. putextra ("MSG", "received Dynamic Registration broadcast successful! "); <Br/> sendbroadcast (intent); <br/>}< br/> // send a system dynamically registered broadcast message. When the mobile phone is connected to the charging device, the system sends a broadcast message. <Br/> else if (v. GETID () = R. id. send_system) {<br/> log. E ("mainactivity", "Sending System Dynamic register broadcast message"); <br/> intent = new intent (); <br/> intent. setaction (systemaction); <br/> intent. putextra ("MSG", "charging .... "); <Br/>}< br/> @ override <br/> protected void onstart () {<br/> super. onstart (); <br/> log. E ("mainactivity", "register broadcast event"); <br/> // register custom dynamic broadcast message <br/> intentfilter filter_dynamic = new intentfilter (); <br/> filter_dynamic.addaction (dynamicaction); <br/> registerreceiver (dynamicreceiver, filter_dynamic ); <br/> // register the system dynamic broadcast message <br/> intentfilter filter_system = new intentfilter (); <br/> filter_system.addaction (systemaction); <br/> registerreceiver (systemreceiver, filter_system); <br/>}< br/> private broadcastreceiver dynamicreceiver = new broadcastreceiver () {</P> <p> @ override <br/> Public void onreceive (context, intent) {<br/> log. E ("mainactivity", "receive custom dynamic register broadcast messages"); <br/> If (intent. getaction (). equals (dynamicaction) {<br/> string MSG = intent. getstringextra ("MSG"); <br/> toast. maketext (context, MSG, toast. length_short ). show (); <br/>}< br/>}; <br/> private broadcastreceiver systemreceiver = new broadcastreceiver () {</P> <p> @ override <br/> Public void onreceive (context, intent) {<br/> log. E ("mainactivity", "Receiving System Dynamic Registration broadcast messages"); <br/> If (intent. getaction (). equals (systemaction) {<br/> string MSG = intent. getstringextra ("MSG"); <br/> toast. maketext (context, MSG, toast. length_short ). show (); <br/>}< br/>}; <br/>}

Step 3: receive broadcast messages. The following are two static registered broadcast receivers.

/** <Br/> * Custom static registration broadcast message receiver <br/> * @ author zuolongsnil <br/> */<br/> public class staticreceiver extends broadcastreceiver {</P> <p> @ override <br/> Public void onreceive (context, intent intent) {<br/> string MSG = intent. getstringextra ("MSG"); <br/> toast. maketext (context, MSG, toast. length_short ). show (); <br/>}< br/>}

/** <Br/> * The system registers the broadcast message receiver statically <br/> * @ author zuolongsnil <br/> */<br /> public class systemreceiver extends broadcastreceiver {</P> <p> @ override <br/> Public void onreceive (context, intent intent) {<br/> If (intent. getaction (). equals (intent. action_battery_low) {<br/> log. E ("systemcycler", "Low Power prompt"); <br/> toast. maketext (context, "your cell phone is underpowered, please recharge in time", toast. length_short ). show (); <br/>}< br/>}

The androidmanifest. xml file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. byread "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <br/> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". mainactivity "Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <Cate Gory Android: Name = "android. Intent. Category. launcher"/> <br/> </intent-filter> <br/> </activity> <br/> <! -- Register a custom static broadcast Receiver --> <br/> <receiver er Android: Name = ". staticreceiver "> <br/> <intent-filter> <br/> <action Android: Name =" com. byread. static "/> <br/> </intent-filter> <br/> </er ER> <br/> <! -- Register the static broadcast receiver of the system --> <br/> <receiver er Android: Name = ". systemcycler "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. battery_low "/> <br/> </intent-filter> <br/> </receiver> <br/> </Application> <br/> </manifest>

Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <textview Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" Android: TEXT = "@ string/Hello"/> <br/> <button Android: Id = "@ + ID/send_static" Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: text = "Send custom static registration broadcast"/> <br/> <button Android: Id = "@ + ID/send_dynamic" Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: text = "Send custom Dynamic Registration broadcast"/> <br/> <button Android: id = "@ + ID/send_system" Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: TEXT = "Sending System Dynamic Registration broadcast"/> <br/> </linearlayout>

The explanation is over, but I have not figured it out myself. If I use sendbroadcast in the program, this system broadcast event is a custom broadcast. If this parameter is left blank, will the system itself send the corresponding action broadcast? If you have any knowledge, please let me know and thank you first.

 

Running interface:

 

 

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.