How to start android service on installation
Asked Answered
S

2

44

I have a device management application, which essentially runs as a service in the background from boot. I'd like to start this application immediately after installation. How do I achieve this?

Spam answered 24/1, 2010 at 12:40 Comment(0)
P
54

You cannot do this -- there is no way to automatically start your service merely because it was installed.

The application must first be invoked by the user through some sort of activity. Or, you are going to need to hook into some relevant broadcast Intent via the manifest, so you can get control when one of those events occur and kick off your service that way. Or, you are going to need to ask the user to reboot so your BOOT_COMPLETED Intent filter can get control.

Passing answered 24/1, 2010 at 13:21 Comment(8)
Thanks, I suspected this might be the case. Seems like a bit of an oversight given how well thought out the OS is generally.Spam
It's a security thing, as far as I understand it. It is definitely not an oversight, as the core Android team has commented on this request several times in the past.Passing
Yes, it is deliberate. Application installation happens automatically in the background when the app is downloaded. We prefer that no app code run until the user explicitly goes to the app the first time. (And please please do not tell the user to reboot their device. There is no need for that. Just let them run your app and do your initialization when first run.)Melville
Ok, so it isn't really for security since I can run code on boot, or on an incoming SMS, or on the next package install. I would classify this more in the category of a "nudge" towards expected platform behaviour. To my customers though, it just looks like a bug - on Android there is an extra hoop to jump through to get the service running. This is not an application that a user would select from the Market, this is more likely the kind of application that an operator or enterprise would want to install on the handset, preferably without any end user interaction at all.Spam
If an operator is installing it on the handset, the phone will be rebooted before it gets to the user, simply because operators don't keep their inventory of phones running -- they'll be shut off after customization and before delivery to a customer. With respect to the enterprise, Android's present form is designed for consumers, and so there will be various things in the platform that will run counter to what an enterprise will want, in the interests of protecting consumers from malware.Passing
Lookout Plan B does it (probably with some hacking) market.android.com/details?id=com.lookout.labs.planb&hl=enDjambi
@amit: Plan B does not work, as of Android 3.1. It will on earlier versions of Android, but the technique they are exploiting has since been blocked.Passing
@CommonsWare: What is the technique that Plan B was exploiting?Explant
C
11

There was a hole - the Android Analytics SDK used to send an intent right after installation - but that got closed (producing lots of confusion, of course).

But the final answer, I believe, is here:

http://developer.android.com/about/versions/android-3.1.html#launchcontrols

This seems to suggest that, as of 3.1, Google made the decision that apps are in a stopped state until the user explicitly activates them, e.g. by launching app or placing widget.

This means that the strategy of listening of a common broadcast (i.e. to get your app launched surreptitiously) won't work either.

Companionship answered 13/9, 2012 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.