Hi All,
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
Hi All,
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
Check out my answer to this question. And these links.
http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/
http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android
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);
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.