Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BOOT_COMPLETE and ACTION_SHUTDOWN never call the BroadcastReceiver

I want to catch ACTION_SHUTDOWN and BOOT_COMPLETE using BroadcastReceiver. But it turns out both signals never trigger the BroadcastReceiver (I didn't see any log on logcat). Here is my source code.

I give the permission on Manifest

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

and I try to register the BroadcastReceiver in both ways

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 are as

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 unregister both BoradcastReceiver in onDestroy

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

Does anyone know what's wrong with my code? Or anything I miss? Thank you.

like image 312
iscnorthwind Avatar asked Mar 20 '23 18:03

iscnorthwind


1 Answers

I found out why it didn't work. Since I use a HTC device, the broadcast messages are different from others.

  1. Shut down event broadcasts "com.htc.intent.action.QUICKBOOT_POWEROFF"
  2. Restart(reboot) event broadcasts "android.intent.action.ACTION_SHUTDOWN"
  3. Power on event broadcasts "com.htc.intent.action.QUICKBOOT_POWERON"

In other device, when shutting down the device, it might broadcast "android.intent.action.QUICKBOOT_POWEROFF".

like image 139
iscnorthwind Avatar answered Apr 08 '23 15:04

iscnorthwind