Question

I want to set up a BroadcastReceiver to handle PACKAGE_REPLACED, but I only want to be notified when my package is replaced. It works fine if I specify a data tag with scheme="package" but then I get notified when any application is reinstalled or upgraded. I can't figure out how I'm supposed to specify the package name which is in the scheme specific part -- that is, when my BroadcastReceiver gets the intent in onRecieve(), if I do intent.getData().getSchemeSpecificPart(), that's the package name, but is there any way I can filter for only that in my intent-filter?

Was it helpful?

Solution

Easiest would be to accept any broadcast intents with the package scheme as you say, but do the filtering based on the Intent extras that are sent with the PACKAGE_* actions.

If the package name in the Intent doesn't match yours, just ignore it.

OTHER TIPS

As used in Google Search App:

<intent-filter>
  <action android:name="android.intent.action.PACKAGE_REPLACED" /> 
  <data android:scheme="package" android:path="com.google.android.googlequicksearchbox" /> 
</intent-filter>

But according to my rough test, this can still catch any updated package, which renders the "android:path" part actually useless.

For Android 3.1+, you can use this new action to respond only when your package updated:

<intent-filter>
  <action android:name="android.intent.action.MY_PACKAGE_REPLACED" /> 
</intent-filter>

If you have to support back to Android 2.x, a better practice is to declare 2 separate receivers for PACKAGE_REPLACED and MY_PACKAGE_REPLACED. Then upon first run, check the runtime Android version and disable the old one if version is 3.1+.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top