Autostart an application at bootup

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

#
Mark Murphy - May 25, 2009 at 10:56 p.m.

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.

#
Markus - May 26, 2009 at 9:18 a.m.

"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 ;)

#
chris - June 28, 2009 at 10:19 a.m.

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!

#
Raj - June 29, 2009 at 8:03 a.m.

Can get the full source code of this snippets,because i have many doubts in it.

#
Markus - July 27, 2009 at 9:22 a.m.

@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 ;(

#
iPaul Pro - October 26, 2009 at 3:44 a.m.

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.

#
watersky - November 25, 2009 at 6:23 a.m.

<receiver> must be under <application>

#
fit flops - December 27, 2009 at 8:49 p.m.

Thanks for sharing

Add a Comment