In this example, a broadcast that is dynamically created by a broadcast receiver that is not defined in the Androidmanifest.xml file is received
Code:
1 PackageCom.qf.broadcastreceiver01;2 3 Importandroid.app.Activity;4 ImportAndroid.app.AlertDialog;5 ImportAndroid.content.BroadcastReceiver;6 ImportAndroid.content.Context;7 Importandroid.content.Intent;8 ImportAndroid.content.IntentFilter;9 ImportAndroid.os.BatteryManager;Ten ImportAndroid.os.Bundle; One ImportAndroid.util.Log; A ImportAndroid.widget.TextView; - - /** the * Receiving system broadcast (three main elements: the action of the broadcast, the permission to receive the broadcast, the data contained in the broadcast) 1) - * Custom broadcast receiver class, Inherit Broadcastreceiver, and override OnReceive () Method 2) - * Register a broadcast receiver while declaring a broadcast channel (Action) 3 that the broadcast receiver can receive via Intentfilter - * Implement related actions in the broadcast received method (note: Executed in the UI thread) + * - * @authorApple + * A */ at Public classMainactivityextendsActivity { - -Myreciver Myreciver;//object declaring a broadcast sink - - TextView TV; - in @Override - protected voidonCreate (Bundle savedinstancestate) { to Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); - theTV =(TextView) Findviewbyid (r.id.msgid); * $ //instantiate broadcast receivers and Intentfilter (function: Announcements can be received by broadcast)Panax NotoginsengMyreciver =Newmyreciver (); - theIntentfilter filter =NewIntentfilter (intent.action_battery_changed); + //increase the number of broadcast channels (Action) that can be received AFilter.addaction (Intent.action_battery_low);//Low-Power broadcast theFilter.addaction (intent.action_power_connected);//Power-connected broadcasts +Filter.addaction (intent.action_power_disconnected);//Power-Disconnected broadcasts - $ //2. Registering a broadcast receiver-power change broadcast of the receiving system $ Registerreceiver (myreciver, filter); - } - the - /**Wuyi * 1. Custom Broadcast Receivers <br> the * The following functions can be implemented in the broadcast receiver's method of receiving broadcasts:<br> - * <li> Start activity Wu * <br><font color=blue> Note: In a global broadcast receiver, the activity must be started with Intent.flag_new_task - * </font> About * <li> Display dialog box, but activity must exist $ * <li> Start service - * <li> Send notifications - * <li> Popup toast - * A * @authorApple + * the */ - $ classMyreciverextendsBroadcastreceiver { the @Override the Public voidOnReceive (Context context, Intent Intent) { the //TODO method of receiving broadcasts--is a method executed in the UI thread the //3. Get the data in the broadcast and display it to the UI control -String action = Intent.getaction ();//get the action for the broadcast inLOG.D ("Debug", "---->" +action); the the if(Action = =intent.action_battery_changed) { About //Get the level information (percentage) of the battery the intLevel = Intent.getintextra (batterymanager.extra_level, 0); the theTv.settext ("Current Power (percentage):" +Level ); + if(level<10){ - //Pop-up dialog box with low battery the NewAlertdialog.builder (mainactivity. This)Bayi. Settitle ("hint") the . SetIcon (R.drawable.ic_launcher) the. Setmessage ("Current low power, please plug in or power off ....") -. Setpositivebutton ("OK",NULL) -. Setnegativebutton ("Cancel",NULL) the . Create (). Show (); the } the the}Else if(action==intent.action_power_connected) { -Tv.settext ("Power is connected:" ); the}Else if(action==intent.action_power_disconnected) { theTv.settext ("Power is disconnected:" ); the //starting the Infoactivity component94Intent infointent=NewIntent (context,infoactivity.class); the context.startactivity (infointent); the the}Else{98Tv.settext ("Low battery!!!" ); About } - }101 }102 103}
Mainactivity.java
Broadcastreceiver broadcast-------receiving power changes dynamically creating receivers in code