BOOT_COMPLETE and ACTION_SHUTDOWN are never called BroadcastReceiver

I want to catch ACTION_SHUTDOWN and BOOT_COMPLETE using BroadcastReceiver. But it turns out that both signals never start BroadcastReceiver (I have not seen any logcat logs). Here is my source code.

I give permission to manifest

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

and I'm trying to register BroadcastReceiver in both directions

 protected void onCreate(Bundle savedInstanceState) { registerReceiver(BootReceiver, new IntentFilter(Intent.ACTION_BOOT_COMPLETED)); registerReceiver(ShutDownReceiver, new IntentFilter(Intent.ACTION_SHUTDOWN)); } <receiver android:name=".BootReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> 

and the source code for BootReceiver and ShutDownReceiver is

 private BroadcastReceiver BootReceiver = new BroadcastReceiver() { private String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(ACTION_BOOT)){ //my stuff Log.d("Power", "Boot Complete"); } } }; private BroadcastReceiver ShutDownReceiver = new BroadcastReceiver() { private String ACTION_SHUTDOWN = "android.intent.action.ACTION_SHUTDOWN"; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ACTION_SHUTDOWN)) { //my stuff Log.d("Power", "Shutdown Complete"); } } }; 

also, I unregistered as BoradcastReceiver in onDestroy

 public void onDestroy() { unregisterReceiver(BootReceiver); unregisterReceiver(ShutDownReceiver); super.onDestroy(); } 

Does anyone know what happened to my code? Or am I missing something? Thanks.

+1
source share
4 answers

I found out why this did not work. Since I use an HTC device, broadcast messages are different from others.

  • Turn off event broadcasts " com.htc.intent.action.QUICKBOOT_POWEROFF "
  • Transfer of reload (reboot) events " android.intent.action.ACTION_SHUTDOWN "
  • Enabling event broadcast " com.htc.intent.action.QUICKBOOT_POWERON "

In another device, when the device is turned off, it can broadcast " android.intent.action.QUICKBOOT_POWEROFF ".

+4
source

Most likely, your application has not yet been added to the list of possible receivers "BOOT_COMPLETED", starting with Android 3.1, in order to receive the action "BOOT_COMPLETED", your application must be opened by the user by the user, or displaying Activity or another component, as long as your application If you don’t get the broadcast that you expect, it’s important to know that if you “force close” the application, it will skip the broadcasts again, so try opening the event and then rebooting your device, you will get it ...

Hope this helps!

Hello!

0
source

Try it.

  <receiver android:name="packagename.GPSReceiver" android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" > <intent-filter android:priority="500" > <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> 
0
source

BOOT_COMPLETED must be registered in the manifest. You cannot register for it through registerReceiver() , because by the time registerReceiver() called, the download will be a long time ago.

AFAIK turning off broadcasting works with registerReceiver() , although in your case it will only be when your process starts.

0
source

Source: https://habr.com/ru/post/1487524/


All Articles