6

I've created an App reminding people to take madications on time. Every time, after I place a new APK on the Google Play, I get many complaints the alarm doesn't work anymore. It begins working again only after the user starts the App (or reboots). Help!

Rotem
  • 89
  • 1
  • 1
  • 3
  • Your app would have been stopped in order to update it, including any services and alarms. You're going to need to tell them to open the app after updating, which will give you a chance to start any services/alarms you need. – Gabe Sechan Jul 22 '13 at 04:49
  • Yeah you are pretty much stuck here. http://stackoverflow.com/questions/8531926/how-to-start-a-service-when-apk-is-installed-for-the-first-time/8535062#8535062 – Phuong Nguyen Jul 22 '13 at 05:28

3 Answers3

9

Check out the ACTION_MY_PACKAGE_REPLACED intent action (on honeycomb and up).

You should be able to register a broadcast receiver for it in the manifest that can then reschedule your alarms.

orip
  • 73,323
  • 21
  • 116
  • 148
  • In Studio 1.0 when I use context help I see only this android.intent.action.PACKAGE_REPLACED . I am not seeing android.intent.action.ACTION_MY_PACKAGE_REPLACED . Is it okay to use first one? I am supporting only after API 16 – user2731584 Jan 14 '15 at 15:09
  • Is your minSdkVersion 16 and your targetSdkVersion 21? – orip Jan 14 '15 at 15:27
  • When I check logcat of the mobile that I use for development I see the following line : Received broadcast action=android.intent.action.PACKAGE_REPLACED . But my receiver class is not getting invoked after this. I am at a loss as to what to do. Please guide. – user2731584 Jan 14 '15 at 17:46
  • 3
    PACKAGE_REPLACED is for other packages, not yours. The string itself is android.intent.action.MY_PACKAGE_REPLACED (no ACTION_ prefix) and should be available as `Intent.ACTION_MY_PACKAGE_REPLACED`. Don't forget to add it to your receiver's intent filter: – orip Jan 14 '15 at 20:29
  • Do you mean to say that I should add ? Your last comment is a bit confusing :) – user2731584 Jan 15 '15 at 07:34
  • Look at your broadcast receiver's intent filter, defined in AndroidManifest.xml. It should specify that you're interesting in intents with the MY_PACKAGE_REPLACED action. e.g check out this example by @CommonsWare: https://github.com/commonsguy/cw-omnibus/blob/master/RestrictedProfiles/App/AndroidManifest.xml#L26-L30 – orip Jan 15 '15 at 09:02
2

Scheduled alarms will NOT be cancelled on upgrades. I verified this for Android versions 2.2 and 4.3 and the latest Google Play at the time of writing.

What you have to make sure is that your new versions can handle all pending intents of any older version. That means:

  • same target definition in the manifest (broadcast receiver or service)
  • same intent "contract": actions, extras, etc.
Jörg
  • 2,441
  • 1
  • 20
  • 18
  • 1
    no actually the alarm will be cancelled, and you have to reset it again – AnasBakez Dec 05 '13 at 05:32
  • 2
    That is incorrect. The alarm will not be canceled any more. Want proof? Look at the source: http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/4.3_r2-robolectric-0/com/android/server/AlarmManagerService.java#AlarmManagerService.UninstallReceiver – sooniln Feb 03 '15 at 22:59
  • 2
    @sooniln you are correct, but your link is pointing to Robolectric source code. Here is the link to the Android source code - http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/com/android/server/AlarmManagerService.java/#1902 – Eliezer Feb 24 '15 at 07:01
  • @Eliezer You're right, I should have been less lazy searching for the source. – sooniln Feb 24 '15 at 20:12
0

My app runs a receiver for android.intent.action.MY_PACKAGE_REPLACED. When the receiver gets an ACTION_MY_PACKAGE_REPLACED Intent, it tears down all previous alarms and sets up new ones (because an update could change the alarm times).

After testing, I've found that:

  • in development, deploying directly to a device using 'adb install -r app.apk', the updated app won't get alarms until I've manually launched the app.
  • after updating via Google Play Store, the updated app gets the alarms without the need for any interaction with the app at all after the update is done.

I wish Google would make this stuff clear - it was a very, very high risk test since if the updated app hadn't got the alarms it would have broken the app for all my users! But I'd tried everything else I could think of and I was 99.9% sure it had to work or how would any app every trust any of its alarms would ever get fired if updates disabled them.

halfer
  • 19,824
  • 17
  • 99
  • 186
user-10884042
  • 31
  • 1
  • 5