Android. intent. action. time_tick registration in the list file does not work for Android. intent. action. time_tick and Android. intent. action. screen_off, and Android. intent. action. screen_on, and Android. intent. action. battery_changed does not work when it is registered in the list file. In this case, you can receive these broadcasts through code registration in a service. For example, I am here to receive the boot broadcast, start a service in this broadcast, and register and receive the broadcast of several actions mentioned above in this service to achieve the effect. The specific code is as follows: 1. Receive the system boot broadcast and enable a service in this broadcast:
Public class bootbroadcastreceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {// todo auto-generated method stub // XXX. class is the service intent service to be started = new intent (context, serviceforbroadcast. class); context. startservice (service); log. V ("tag", "Automatic startup of the service ..... "); system. out. println ("starting service! ");}}
2. The services enabled in the broadcast above are:
Public class serviceforbroadcast extends Service {@ overridepublic ibinder onbind (intent) {// todo auto-generated method stubreturn NULL;} @ overridepublic void oncreate () {// todo auto-generated method stubsuper. oncreate ();/** action for receiving screen changes */intentfilter filter1 = new intentfilter (intent. action_screen_on); filter1.addaction (intent. action_screen_off);/** action for receiving power change broadcast */intentfilter filter2 = new intentfilter (); filter2.addaction (intent. action_battery_changed);/** action for broadcasting when the receiving time changes */intentfilter filter3 = new intentfilter (); filter3.addaction (intent. action_time_tick);/** register screen-changed broadcast, battery-changed broadcast, and time-changed broadcast in the Code */broadcastreceiver mreceiver1 = new screenbroadcast (); required bytes = new batterybroad (); required bytes = new timetickbroadcast (); registerreceiver (Region, filter1); registerreceiver (Region, filter2); registerreceiver (mreceiver3, filter3 );}}
3. Register broadcast in the above service: (three in total)
Public class batterybroad extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {// todo auto-generated method stubsystem. Out. println ("the power has changed! "); // Toast. maketext (context," power changing ", Toast. length_short). Show ();}}
Receive screen change broadcasts:
Public class screenbroadcast extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {// todo auto-generated method stubsystem. Out. println ("the screen is changing! "); // Toast. maketext (context, "screen changing", toast. length_short ). show (); If (intent. getaction (). equals (intent. action_screen_on) {// receives the system notification that the screen is enabled. out. println ("receives screen-enabled broadcasts");} else {// receives the screen-off notification system. out. println ("receives the screen off broadcast! ");}}}
Broadcast that receives time changes:
Public class timetickbroadcast extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {// todo auto-generated method stubsystem. Out. println ("time is changing! "); // Toast. maketext (context," time changing ", Toast. length_short). Show ();}}
4. configuration file:
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. innerbroadcast. main "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "Android: targetsdkversion =" 17 "/> <! -- Obtain Battery status permissions --> <uses-Permission Android: Name = "android. Permission. battery_stats"/> <! -- Permissions required for startup --> <uses-Permission Android: Name = "android. permission. receive_boot_completed "/> <application Android: allowbackup =" true "Android: icon =" @ drawable/ic_launcher "Android: Label =" @ string/app_name "Android: theme = "@ style/apptheme"> <! -- Register the service --> <service android: Name = "com. innerbroadcast. Service. serviceforbroadcast"> </service> <! -- Receives the boot broadcast --> <receiver er Android: Name = "com. innerbroadcast. service. bootbroadcastreceiver "> <intent-filter> <action Android: Name =" android. intent. action. boot_completed "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </explorer> </Application> </manifest>