For my Android project Stopeen, I implemented a functionality to schedule actions with a recurrence over time. But one of my problem was : when people will update my app from the store, the service in background will be killed, and so do they the pending intents handled by the alarm manager. So how could I restart the service without forcing my user to launch my app immediately after each update?
The action to automatically restart service after update
I found this action raised by the system : ACTION_MY_PACKAGE_REPLACED
ACTION_MY_PACKAGE_REPLACED — Notifies an application that it was updated, with a new version was installed over an existing version. This is only sent to the application that was replaced. It does not contain any additional data. To receive it, declare an intent filter for this action. You can use the intent to trigger code that helps get your application back in proper running shape after an upgrade. This intent is sent directly to the application, but only if the application was upgraded while it was in started state (not in a stopped state).
In simpler words, the system can call your app after an update, if the user has opened it at least once before.
Implementation
To use it, first create the following broadcast receiver in your project, by replacing MyServiceImplementation by the name of your service class:
1 2 3 4 5 6 7 8 9 |
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class Launcher extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Intent service = new Intent(context, MyServiceImplementation.class); context.startService( service ); } } |
It will start the service when called. After that, go in your manifest file and add a filter on the action MY_PACKAGE_REPLACED.
1 2 3 4 5 |
<receiver android:name=".Launcher" > <intent-filter> <action android:name="android.intent.action.MY_PACKAGE_REPLACED" /> </intent-filter> </receiver> |
Here you go ! The service will be launched again just after the user update your app, even if he/she does not open the app starting it.
Cases when the service will not restart
This works only if the user has the service running in background when the update occurs, or if the service has stopped by itself earlier.
It will not works if the user has never used an app launching the service yet, or if the user has manually killed the service.
Header picture derived from a TrueMitra‘s picture
By flames 13/04/2016 - 16:25
Very good article but only one thing, the name of the intent is:
android.intent.action.MY_PACKAGE_REPLACED
It made me crazy!!
By Nicolas Form 14/04/2016 - 10:49
Thanks Flames! You are right, I have updated the post.
By Karl 01/05/2018 - 18:33
Hey. Cool thing thanks for sharing. I tried this to instead of a service restart an activity after updating my app (with startActivity instead of startService), but this doesn’t seem to work. Any suggestions how to accomplish this? Thanks in advance!
By Nicolas Form 02/05/2018 - 08:25
Hi Karl, thanks! I guess you are not allowed to do that; if developers could start an activity outside of the app execution, the user would be spammed by many self-opening apps.
By Laszlo 08/05/2018 - 04:11
Works like a charm! Thanks for sharing!