7

i need to perform an action when my application is installed. i've looked into using

Intent.PACKAGE_ADDED

but i don't receive the intent in the app that's being installed. i want to run code when my app is installed for the first time.

the use case is registering with an online service. i can listed for BOOT_COMPLETED which is fine if the app is already installed, but i need to handle the case when the user first installs the app.

this post, Can you run an intent or script when your app gets installed on Android?

suggests listening to TIMER_TICK and on the first broadcast, perform the registration and set a flag so as not to perform it upon the next TIMER_TICK. this seems problematic because whether you do something or not in the receiver, you are still starting your receiver every single minute and using up battery in the process.

is there a better solution?

Community
  • 1
  • 1
Jeffrey Blattman
  • 21,054
  • 8
  • 74
  • 127

3 Answers3

5

There is no reliable event that you can catch. Even TIMER_TICK will only work below Android 3.1. From 3.1. onwards you can't receive system broadcasts until your app is in active state (which means the user has to launch it at least once manually).

Launch controls on stopped applications

Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications.

[...]

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. [..]

Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).

from the 3.1. release notes

I recommend to do your intitial work when the user starts your app for the first time.

Community
  • 1
  • 1
  • I think you have a typo -- shouldn't your first sentence be "There is no reliable even that you can catch"? – CommonsWare Dec 14 '11 at 22:11
  • 1
    Note that if you want to follow alextsc's suggestion of doing your initial work when the user starts your app for the first time, there's a new broadcast exactly for that: [ACTION_PACKAGE_FIRST_LAUNCH](http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_FIRST_LAUNCH). – kabuko Dec 14 '11 at 22:50
1

With the new market, the INSTALL_REFERRER intent is fired at launch time now for Android 3.1 and above. So we can receive it in our app. However it still fires at install time for 2.2 when the app is in stop state. So I am not able to receive it in case of Froyo.

aasha
  • 386
  • 3
  • 10
0

If you're willing to assume that the Google Android Market app is installed, you could use INSTALL_REFERRER. See Get referrer after installing app from Android Market for more info.

Community
  • 1
  • 1
kabuko
  • 35,009
  • 7
  • 75
  • 92
  • This should no longer work as of Android 3.1. You cannot receive any broadcasts until the user launches your application manually the first time. – CommonsWare Dec 14 '11 at 22:06
  • Ah, I wasn't aware of that CommonsWare, thanks. Will that broadcast queue up somehow and be received when the app launches? – kabuko Dec 14 '11 at 22:08
  • Possibly. I haven't researched that one. In general, broadcasts are not queued up, but the Market might do something special for that one -- I'm not sure. – CommonsWare Dec 14 '11 at 22:09
  • Looking at [FLAG_INCLUDE_STOPPED_PACKAGES](http://developer.android.com/reference/android/content/Intent.html#FLAG_INCLUDE_STOPPED_PACKAGES) it looks like it could work if the Market app sets this flag. I'll have to look to see if it does. – kabuko Dec 14 '11 at 22:17
  • Looks like it doesn't. I don't think 3.1+ devices with the current version of the Market app ever get INSTALL_REFERRER. – kabuko Dec 14 '11 at 22:37
  • Considering that `INSTALL_REFERER` was widely used as a workaround to the "app won't run on install" rule, I'm not terribly shocked. – CommonsWare Dec 14 '11 at 22:41