1

The main goal is to get notified about the app getting updated and restart a service after app update.

Having a BroadcastReceiver and listening to Intent.ACTION_PACKAGE_REPLACED  to start my service when the app gets updated. And I get the following error

BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:com.volvocars.dj flg=0x4000010 (has extras) } to com.volvocars.drivingjournal/.core.service.KeepTheAppAliveReceiver

At the first glance, It seems like one of Background Execution Limits,

And I found that I could replace ACTION_PACKAGE_REPLACED by ACTION_MY_PACKAGE_REPLACED the result is that the error goes away but the BroadcastReceiver doesn't get any update event and it doesn't work. How can I fix this?

Morteza Rastgoo
  • 6,772
  • 7
  • 40
  • 61

1 Answers1

0

Ensure your AndroidManifest.xml's broadcast receiver class ClassChild.java has this intent-filter:

 <receiver
            android:name=".subdir.ClassChild"
            android:exported="false"
            android:label="@string/foo>
            <intent-filter>
                 <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
            </intent-filter>

and if let's say the ClassChild may extends classParent, letClassChild override classParent's onReceive():

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
}

And classParent.java's onReceive() can share by child to check the broadcast (you don't put funny thing such as final onReceive() which causes child not able to override):

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (intent == null) {
        return;
    }
    String action = intent.getAction();

    if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(action)) {
        ... do your stuff
    }

Finally, you can test app update with command adb install -r -d <your.apk> if you worry Android Studio may not send MY_PACKAGE_REPLACED broadcast. (-r means re-install and keeping its data, -d means allow version downgrade)

林果皞
  • 7,539
  • 3
  • 55
  • 70