in AndroidManifest.xml (application-part):
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
[..]
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
[..]
public class BootUpReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
8 Comments
Using BOOT_COMPLETED to trigger, say, a service to do something in the background is one thing, and even that was considered bad enough to warrant requiring its own permission.
Using BOOT_COMPLETED to start an Activity, though, is evil incarnate. Users are unlikely to appreciate a bunch of activities deciding they want to take control of the device after a reboot. Anyone using this deserves whatever lousy Android Market rating they get.
"Anyone using this deserves whatever lousy Android Market rating they get."
thanks :P
I used this code because we are developing an application which is sold with an android-device as a bundle. So we have an interest in starting an activity at boot-up without being evil ;)
Another way you could use this code is if you package your app with an emulator and want your app to autostart.
Great snippet, thanks!
Can get the full source code of this snippets,because i have many doubts in it.
@Raj: Sorry I didn't see your post ... I'm afraid I can't offer any complete code because it's part of a project we're developing at our company and we're not into open-source currently ;(
While I would use extreme discretion in having an activity auto-start at boot, there are some instances where a service is necessary.
All of my currently published apps deal with issues of status bar notifications and alarms being lost with reboot. It is absolutely necessary for my apps to run a service, at boot, that checks for "active" notifications and alarms, restores them, and then attempts to kill itself.
I have not seen any evidence of leaks or hangs in my boot services, nor do they seem to affect performance much, if at all. That said, the services, in question, are relatively inexpensive anyway.
<receiver> must be under <application>
Thanks for sharing
Add a Comment