My app runs a service which is terminated when the device reboots or the app is reinstalled (updated). I've added two broadcast receivers to catch those events - BOOT_COMPLETED and ACTION_MY_PACKAGE_REPLACED.
The ACTION_MY_PACKAGE_REPLACED receiver just doesn't seem to work. Here's what I have:
AndroidManifest.xml:
<receiver android:name=".RebootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".ReInstallReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
RebootReceiver:
public class RebootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Logg.d("Reboot completed. Restarting service");
context.startService(new Intent(context, MyService.class));
}
}
ReInstallReceiver:
public class ReInstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Logg.d("App Upgraded or Reinstalled. Restarting service");
context.startService(new Intent(context, MyService.class));
}
}
Running minSdk=16; Testing on Galaxy S3 running KitKat. Testing success by checking if my service is running in Settings/Applications, which it does on reboot, but not reinstall.
I've taken into account notes from the following, which say that in Android Studio 1.0+, manifest mergers mean I can't combine two receivers into one class. See ACTION_MY_PACKAGE_REPLACED not received and Android manifest merger fails for receivers with same name but different content
You probably figured this out already, but your action name in the manifest is wrong, instead of:
android.intent.action.ACTION_MY_PACKAGE_REPLACED
it should be
android.intent.action.MY_PACKAGE_REPLACED
You can also manually trigger the receiver using adb shell
for testing purposes:
adb shell am broadcast -a android.intent.action.MY_PACKAGE_REPLACED -n com.example.myapp/.ReInstallReceiver