10

Anytime I publish a new version of my app in the Market, if the user had enabled the "auto update" option, the app will be updated automatically.

The app contains a service that runs constantly. But when the automatic update happens, the old running app is killed, but the new one is not started. Since the update happens mostly transparently to the user, it makes sense that the app's service should be started again automatically after the update so that there is almost no interruption of the service.

It's a bit difficult to test this with a real update from the market, so I'm using the following two adb commands to simulate this update process. Install of the 1st version:

adb install oldversion.apk

Automatic update:

adb install -r newversion.apk

After I run the second command, the app gets successfully updated, but it has been stopped and not restarted.

How can we make the new version's service start automatically?

gpo
  • 3,388
  • 3
  • 31
  • 53
  • 1
    http://stackoverflow.com/questions/10728016/android-restart-application-after-update-action-package-replaced – Alexis Oct 10 '13 at 00:25

3 Answers3

9

If your app is running on API 12 or higher, I would recommend registering a BroadcastReceiver listening to android.intent.action.MY_PACKAGE_REPLACED.

This Intent is only triggered if YOUR application got an update.

janwo
  • 754
  • 2
  • 8
  • 17
5

It's something you'll have to test carefully but you should be able to catch it with a broadcast receiver with the action Intent.ACTION_PACKAGED_REPLACED

Then you start the service from your receiver.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • I am trying But Not Working... I tried http://pastie.org/8223893 Can You Give Suggestion?? – Karan Mavadhiya Aug 10 '13 at 07:19
  • I think using ACTION_MY_PACKAGE_REPLACED will be better ? As it is just being sent to the package being updated, not all packages. – code2be Jan 27 '16 at 05:16
  • As the android document https://developer.android.com/guide/components/broadcasts Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead. So this Intent.ACTION_PACKAGED_REPLACED is still working ? – Vo Thanh Tung Nov 25 '19 at 07:35
1

Register a BroadcastReceiver to Intent.ACTION_PACKAGE_REPLACED

Then, compare EXTRA_UID with your own. If it matches, you can start your service again.

njzk2
  • 38,969
  • 7
  • 69
  • 107