简体   繁体   中英

Stop BroadCastReceiver from triggering on Application update in Android

I have to show prompt when a new Application is installed or uninstalled in the device,so far its working fine. The only problem is prompt is coming even when Application is updated. How to stop BroadCastReceiver from triggering on Application update.

<receiver android:name=".WeepingReceiver">
    <intent-filter android:priority="100">
         <action android:name="android.intent.action.PACKAGE_ADDED" />
         <action android:name="android.intent.action.PACKAGE_REMOVED" />

         <data android:scheme="package" />
    </intent-filter>
</receiver>

BroadCast

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_PACKAGE_INSTALL)
            || intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            context.startActivity(new Intent(context, NewAppActivity.class).
            setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra(Utility.NEW_PACKAGE_NAME, packageName));
}

Try this

Bundle extras = intent.getExtras();
if (extras.containsKey(Intent.EXTRA_REPLACING) && extras.getBoolean(Intent.EXTRA_REPLACING))
{
//do nothing here it is condition of updating your existing app
}else
{
//do your code here
}

Just try to change your if condition as

if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) &&
       (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED))) {

      context.startActivity(new Intent(context, NewAppActivity.class)
          .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
          .putExtra(Utility.NEW_PACKAGE_NAME, packageName));
}

And manifest

<receiver android:name=".WeepingReceiver">
    <intent-filter android:priority="100">
         <action android:name="android.intent.action.PACKAGE_ADDED" />
         <action android:name="android.intent.action.PACKAGE_REMOVED" />
         <action android:name="android.intent.action.PACKAGE_REPLACED" />
         <data android:scheme="package" />
    </intent-filter>
</receiver>

Application Update is re-installing a new app so it is correct that your receiver receives the event as PACKAGE_ADDED . Thus, you cannot stop your broadcast from receiving the event.

However, you can validate if the intent is being updated by checking whether the package name was there before. You can have a list of installed apps' package names and store. Then check as you are doing:

if (intent.getAction().equals(Intent.ACTION_PACKAGE_INSTALL)
            || intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)
            || !packageList.contains(packageName))

You could get your package list by:

final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) {
   packageList.add(packageInfo.packageName);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM