7

I know how to autostart after boot with the BOOT_COMPLETED intent action, but I didn't find how to autostart an application just after it has been installed on the device.

For my application I would like to set an alarm after the install, I looked at the PACKAGE_ADDED intent action but it says that the newly installed package does not receive this broadcast.

Any advice ?

Thanks in advance

tbruyelle
  • 12,895
  • 9
  • 60
  • 74

1 Answers1

8

As you mention, there's no way of receiving your own PACKAGE_ADDED event; you just have to check for a flag each time you start your application.

For example:

SharedPreferences prefs = getPreferences(MODE_PRIVATE);
if (!prefs.contains(KEY_FIRST_RUN)) {
    /* do some one-off stuff here */
    prefs.edit().putBoolean(KEY_FIRST_RUN, false).commit();
}

You could put this in your Application class, or in your launcher's onCreate method.

Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • Ok thank you, I understand why I didn't find anything about that ! What I would like is that to set the alarm without starting my application, but I think it's not possible... So there is no way to auto-launch an application apart after boot... – tbruyelle Dec 29 '09 at 16:52
  • 1
    "So there is no way to auto-launch an application apart after boot" -- correct. That is by design. – CommonsWare Dec 29 '09 at 16:57
  • thanks all, I consider Christopher's response as an acceptable answer so ;-) – tbruyelle Dec 29 '09 at 17:01
  • Indeed, it is by design, not that I quite agree with the decision. If developers are trusted not to abuse the `BOOT_COMPLETED` broadcast, why not the `PACKAGE_ADDED` one? – Christopher Orr Dec 29 '09 at 17:03
  • Especially that a `PACKAGE_ADDED` broadcast is triggered at each install of any application... – tbruyelle Dec 29 '09 at 17:27