3

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));
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42

3 Answers3

3

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
}
Mohd Saquib
  • 580
  • 4
  • 14
  • The above solution worked for me thanks. But now I am getting the activity when app is uninstalled. How to handle that ? Please tell –  Aug 04 '17 at 11:12
  • Just check remove condition as : if (extras.containsKey(Intent.ACTION_PACKAGE_REMOVED) && extras.getBoolean(Intent.ACTION_PACKAGE_REMOVED)){ } and do nothing in this condition or write your condition as suitable to you. – Mohd Saquib Aug 04 '17 at 12:32
0

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>
Louis CAD
  • 10,965
  • 2
  • 39
  • 58
Mohd Saquib
  • 580
  • 4
  • 14
0

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);
}
Jisu Hong
  • 724
  • 1
  • 7
  • 22