Reputation: 3732
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
Upvotes: 14
Views: 15782
Reputation: 1167
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.
Upvotes: 17
Reputation: 91
Take into account that:
Upvotes: 9
Reputation: 3679
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
Upvotes: 23