0

Is there a way that trough the application, I can subscribe and unsubscribe for the "ACTION_BOOT_COMPLETED" message?

If so, how can I do this?

Any kind of pointer will help me.

Thanks in advance,
Regards,
Vinay

Vinay
  • 2,395
  • 3
  • 30
  • 35

3 Answers3

1

The best way to do this is use PackageManager.setComponentEnabledSetting(): http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting(android.content.ComponentName, int, int)

So simply decide whether you want your receiving to be enabled or not by default by setting android:enabled in the manifest. Then use this API to explicitly enable or disable the component at run time as desired. If the component is disabled, at boot it will not be available, and thus not receive the broadcast.

hackbod
  • 90,665
  • 16
  • 140
  • 154
0

As a matter of fact, there is:

Your boot receiver:

public class BootstrapReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context cntxt, Intent intent) {
      String action = intent.getAction();

      if(Intent.ACTION_BOOT_COMPLETED.equals(action)) {
          // do your thing
      }     
   }
}

Add a receiver and a permission for receiving boot events:

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

  <receiver android:name=".BootstrapReceiver" android:enabled="true">
     <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
     </intent-filter>
  </receiver>

Via the application (Use this if you want to programatically add/remove boot receiver and not via AndroidManifest.xml. But remember, you will still have to declare the boot permission in your AndroidManifest.xml)

  Context ctx = getContext();
  BootstrapReceiver booter = new BootstrapReceiver();
  IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
  ctx.registerReceiver(booter, filter);

Unregister:

  ctx.unregisterReceiver(booter);
naikus
  • 24,302
  • 4
  • 42
  • 43
  • 1
    Your second part doesn't work. By definition BOOT_COMPLETED is only sent when the system first boots... before your app has run... so there is no opportunity to call registerReceiver() to tell it about the receiver before the broadcast is sent. – hackbod Aug 12 '10 at 08:37
  • @hackbod Its quite obvious to mention here, it will work only on next boot – naikus Aug 12 '10 at 08:51
  • 1
    No, it won't work. The registerReceiver() only lasts for the lifetime of the process. That is why there is no need to have BootstrapReceiver in your manifest -- this just sets up a direct callback to the Receiver instance you provide. Once the instance goes away (when your process goes away), the registration is gone. – hackbod Aug 12 '10 at 21:20
  • 1
    @hackbod, You are right! I checked the documentation and there seems to be no way to do that. My bad :) – naikus Aug 13 '10 at 10:08
  • 1
    Yes there is a way to do that: it's called `PackageManager.setComponentEnabledSetting`. – Christopher Orr Aug 17 '10 at 22:42