tags:

views:

353

answers:

2

Hi all,

I noticed that an alarm is disabled when the application which sets this alarm has been upgraded. Is that true ?

Until now, I used the SharedPreferences with a FIRST_RUN key in order to know if it's the first run of my application. If I don't find this key, I enable the alarm and set FIRST_RUN to false, else I do nothing.

But I noticed also that these preferences remain intact between app upgrade !

So after an upgrade, the FIRST_RUN key is already false, so I do nothing while my alarm need to be enabled.

How to handle such case ?

Thanks in advance

+6  A: 

I've never tried this myself, but what about creating a BroadcastReceiver that listens to the ACTION_PACKAGE_REPLACED Intent?

I've thought about trying this before, but I'm not sure if there's a chicken-and-egg problem with it or not (e.g., does the Intent get sent before the new upgraded application can receive it?). Worth a try, though.

Daniel Lew
Yes it works ! Thanks Daniel, actually the intent is sent just after the upgrade so my receiver is able to catch it. I'm just wondering now if my receiver will be triggered at each upgrade of any application ?
kosokund
It will trigger each time you upgrade, however in the docs it says that you can look at the Intent's data and it will tell you the name of the package. Therefore, you can use intent filters to filter out all package replacements that aren't your own.
Daniel Lew
+5  A: 

Solution by Daniel Lew :

Need a receiver with the following lines in manifest :

<receiver android:name=".OnUpgradeReceiver">
  <intent-filter>
    <action android:name="android.intent.action.PACKAGE_REPLACED" />
    <data android:scheme="package" android:path="your.app.package" />
  </intent-filter>
</receiver>

android:path is used in order to prevent OnUpgradeReceiver to be triggered by any upgrade of any application.

kosokund