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
A simple solution to this problem is to call the registerReceiver() in your Custom Application Class. This will ensure that your Broadcast receiver will be called only one in your entire Application lifecycle.
A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.
There are mainly two types of Broadcast Receivers: Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works even if the app is closed. Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized.
To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.
I wanted to update this thread with a new answer, as I have found no posts that offer an updated solution for Android 7.0+ where this Intent
is now protected.
Go to Build -> Build APK
, and note the location that the .apk is stored.
Then, run in terminal:
adb install -r debugapp.apk
This will trigger the MY_PACKAGE_REPLACED
intent, since newer Android SDKs only allow the system to broadcast it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With