How to correctly filter Package replaced broadcast
Solution 1
Add this to your onReceive method:
if (intent.getDataString().contains("com.my.app")){
...
}
EDIT:
Note that registering for ACTION_PACKAGE_REPLACED
causes your app to be started up every time any app is updated, if it wasn't already open. I don't know how to avoid this before API 12, but in API 12 you can register for ACTION_MY_PACKAGE_REPLACED
so you don't have to filter the intent and your app won't be started unnecessarily by other apps being updated.
Solution 2
Alternately, if your code is in a library that's included in multiple apps, or if you just want something that can be copy/pasted between apps without edits:
int intentUid = intent.getExtras().getInt("android.intent.extra.UID");
int myUid = android.os.Process.myUid();
if (intentUid == myUid)
{
...
}
ninjasense
Updated on June 03, 2022Comments
-
ninjasense almost 2 years
I am trying to catch the package replaced broadcast for my app and only my app, but for some reason in my reciever I am the broadcast for every app that is updated. I thought you only needed to set the intent filter in the manifest file to your app, but maybe I am wrong?
Here's my code(manifest):
<receiver android:name=".UpdateReciever"> <intent-filter> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <data android:scheme="package" android:path="com.my.app" /> </intent-filter> </receiver>
Reciever:
public class AppUpdateReciever extends BroadcastReceiver { @Override public void onReceive(Context con, Intent intent) { //code.. } }
-
android developer over 10 yearsso there is no way to register only to intents of the current app? it seems a google developer (named "Dianne Hackborn") thinks it is possible : osdir.com/ml/Android-Developers/2009-11/msg04736.html
-
Tenfour04 over 10 yearsThat implies that there was a way, but I never figured it out. In Honeycomb, you can use
ACTION_MY_PACKAGE_REPLACED
to avoid the problem withACTION_PACKAGE_REPLACED
: developer.android.com/reference/android/content/… -
android developer over 10 yearsyes, but I can't find the older way, so what i've done is a fallback which uses both methods, as i've shown here: stackoverflow.com/a/21510561/878126 .