Scott
Scott

Reputation: 3732

Android: Broadcast ACTION_MY_PACKAGE_REPLACED never received

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

Answers (3)

Bryan W
Bryan W

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

GrayMemeroy
GrayMemeroy

Reputation: 91

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

Upvotes: 9

G. Lombard
G. Lombard

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

Related Questions