12

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

Community
  • 1
  • 1
Scott
  • 3,663
  • 8
  • 33
  • 56

3 Answers3

21

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
G. Lombard
  • 3,569
  • 1
  • 29
  • 30
  • 15
    can't emulate this intent using adb command on Android 7 Nougat, seems that now it's in the list of sensitive intents: "java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MY_PACKAGE_REPLACED". Works perfectly on Kit-Kat 4.4 though, even without adb command it calls my breakpoint in receiver every time I start the app in Android Studio – Kirill Karmazin Apr 17 '17 at 21:41
16

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.

Bryan W
  • 1,112
  • 15
  • 26
  • Does this work even if the re-installed APK is identical to the one that was originally installed? Or is it necessary to increment the version code? – Alex von Brandenfels Aug 22 '19 at 21:50
  • 1
    @AlexvonBrandenfels, Version code is only necessary to increment when pushing a signed package to Firebase Test Lab or Google Play, the package replaced flag re-installs the same app while triggering the replacement intent. – Bryan W Aug 23 '19 at 17:06
  • 1
    @AlexvonBrandenfels no, i had to manually install the apk using adb using -d (downgrade, add this) option. then you can use the "run app" button in android studio to deploy to emulator and it should trigger MY_PACKAGE_REPLACED. – chitgoks Nov 12 '19 at 02:25
9

ACTION EXPLAIN IMAGE

Take into account that:

  1. A new version of your Application
  2. You should run adb install -r your new version apk,if you just run at Android Studio it won't receive this broadcast
loki
  • 9,816
  • 7
  • 56
  • 82
GrayMemeroy
  • 91
  • 1
  • 2
  • -1 , I'm testing an app on Android 10, Specifically the app is https://github.com/M66B/NetGuard It recieves the broadcast even with Android studio install and without new version code. – Zubair Younas Dec 16 '21 at 08:55