Broadcastreceiver (broadcast receiver) is one of the four components of Android.
The following is an overview of Broadcastreceiver in Android Doc:
The ① broadcast receiver is a component that focuses on receiving broadcast notification information and making the appropriate processing. Very many broadcasts originate from system code-for example, notification time zone changes, low battery level, taking a photo, or user changing language options. Applications can also broadcast-for example, notifying other applications that some data has been downloaded and in a usable state.
The ② application can have a random number of broadcast receivers to respond to all of the notification messages that are of interest to them. All receivers inherit from the Broadcastreceiver base class.
The ③ broadcast receiver does not have a real user interface. However, they can initiate an activity to respond to the information they receive, or use Notificationmanager to notify the user. Notifications can be used in a number of ways to attract users ' attention-flashing back lights, shaking, playing sounds, and more. In general, a persistent icon is placed on the status bar where the user can open it and get the message.
There are two kinds of broadcast events in Android, one is the system broadcast event, for example: action_boot_completed (triggered after system boot), action_time_changed (triggered when the system time changes), Action_battery_ Low (triggers when the battery is lower), and so on. The second is our own definition of broadcast events.
The process of broadcasting events
① Register Broadcast event: There are two types of registration, one is static register, It is defined in the Androidmanifest.xml file that the broadcast receiver of the register must inherit the Broadcastreceiver; there is also a dynamic register, which is the use of the Context.registerreceiver register in the program, which is equivalent to an anonymous class. Both of these methods require Intentfilter.
② sends a broadcast event: the action that is used by the intent to transmit the register via Context.sendbroadcast.
③ Receive broadcast event: When the broadcast being sent is heard by the receiver, its onreceive () method is called, and the intent object that contains the message is passed to it. The code in the OnReceive should not run more than 5s, otherwise Android will eject the timeout dialog.
below I demonstrate myself by code the use of defining broadcast events and system broadcast events. Complete code:Android_broadcastreceiver.rar
STEP1: Register the broadcast event in the Mainactivity OnStart method. The static registration method is in the Androidmanifest.xml file.
STEP2: Clicking on the corresponding button will trigger the corresponding way to send the broadcast message.
/** * mainactivity * @author zuolongsnail * */public class Mainactivity extends Activity {private Button Sendstaticbtn;pri Vate button Senddynamicbtn;private button sendsystembtn;private static final String staticaction = "com.byread.static"; private static final String dynamicaction = "com.byread.dynamic";//USB Device Connection private static final string systemaction = Inte Nt. action_power_connected; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate ); Setcontentview (r.layout.main); sendstaticbtn = (Button) Findviewbyid (r.id.send_static); senddynamicbtn = (Button) Findviewbyid (r.id.send_dynamic); sendsystembtn = (Button) Findviewbyid (R.id.send_system); Sendstaticbtn.setonclicklistener (New Myonclicklistener ()); Senddynamicbtn.setonclicklistener (new Myonclicklistener ()); Sendsystembtn.setonclicklistener (new Myonclicklistener ());} Class Myonclicklistener implements onclicklistener{@Overridepublic void OnClick (View v) {//send itself to define static register broadcast message if (V.getid () = = r.id.send_static) {LoG.E ("Mainactivity", "send self-defined static register broadcast message"), Intent Intent = new Intent (); intent.setaction (staticaction); Intent.putextra (" Msg "," Receive static register broadcast successfully! "); Sendbroadcast (intent);} Send yourself define dynamic Register broadcast message else if (v.getid () = = r.id.send_dynamic) {log.e ("mainactivity", "Send own definition dynamic register broadcast message"); Intent Intent = new Intent (); intent.setaction (dynamicaction); Intent.putextra ("MSG", "Receive dynamic Register broadcast success!") "); Sendbroadcast (intent);} Send system dynamic register broadcast messages. A broadcast message is sent by the system itself when the phone is connected to a charging device. else if (v.getid () = = R.id.send_system) {log.e ("mainactivity", "Send system dynamic Register broadcast Message"); Intent Intent = new Intent (); Intent.setaction (systemaction); Intent.putextra ("msg", "Charging ....") ");}}} @Overrideprotected void OnStart () {Super.onstart (); LOG.E ("mainactivity", "Register broadcast event");//Register yourself to define the dynamic broadcast message intentfilter filter_dynamic = new Intentfilter (); filter_ Dynamic.addaction (dynamicaction); Registerreceiver (Dynamicreceiver, filter_dynamic);//Register system dynamic Broadcast message Intentfilter Filter_system = new Intentfilter (); filter_system.addaction (systemaction); Registerreceiver (SystemReceiver, Filter_ System);} Private BroadcastreceivEr dynamicreceiver = new Broadcastreceiver () {@Overridepublic void OnReceive (context context, Intent Intent) {log.e ("Main Activity "," receive self-defined dynamic register broadcast messages "); if (Intent.getaction (). Equals (Dynamicaction)) {String msg = Intent.getstringextra (" msg ") ); Toast.maketext (Context, MSG, toast.length_short). Show ();}}; Private Broadcastreceiver Systemreceiver = new Broadcastreceiver () {@Overridepublic void OnReceive (context context, Intent Intent) {log.e ("mainactivity", "Receive system dynamic register broadcast messages"), if (Intent.getaction (). Equals (Systemaction)) {String msg = Intent.getstringextra ("msg"); Toast.maketext (Context, MSG, toast.length_short). Show ();}};}
STEP3: Receive broadcast messages. The following is a broadcast receiver for two static brochures.
/** * Define static brochure Broadcast message receiver * @author Zuolongsnail * */public class Staticreceiver extends Broadcastreceiver {@Overridepubli c void OnReceive (context context, Intent Intent) {String msg = Intent.getstringextra ("msg"); Toast.maketext (Context, MSG, toast.length_short). Show ();}}
/** * System static Register Broadcast message receiver * * @author Zuolongsnail * */public class Systemreceiver extends Broadcastreceiver {@Overridepublic void OnReceive (Context context, Intent Intent) {if (Intent.getaction (). Equals (Intent.action_battery_low)) {LOG.E (" Systemreceiver "," low battery indication "); Toast.maketext (Context, "Your phone is low in charge, please recharge it in time", Toast.length_short). Show ();}}
The following is the Androidmanifest.xml file:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.byread "android:versioncode=" 1 "android:versionname=" 1.0 "><application android:icon=" @ Drawable/icon "android:label=" @string/app_name "><activity android:name=". 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><!--Register yourself to define a static broadcast receiver--><receiver Android:name= ". Staticreceiver "><intent-filter><action android:name=" com.byread.static "/></intent-filter> </receiver><!--Register system static broadcast receiver--><receiver Android:name= ". Systemreceiver "><intent-filter><action android:name=" Android.intent.action.BATTERY_LOW "/></ Intent-filter></receiver></application></manifest>
Interface Layout File Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@string/ Hello "/><button android:id=" @+id/send_static "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:text=" sends its own definition static register broadcast "/><button android:id=" @+id/send_dynamic "android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:text=" sends its own definition dynamic register broadcast "/><button android:id=" @+id/ Send_system "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" Send system Dynamic Register broadcast "/ ></LinearLayout>
The explanation is over, but one thing I don't know for myself is that this system broadcast event assumes that I am sendbroadcast in the program, and that I define the broadcast myself. If It is not written, is it not the system itself to send the corresponding action broadcast it? If you have any friends, please let me know.
Execution interface:
"Broadcast receiver components in Android specifically explained"