Search code examples
androidbroadcastreceiverandroid-broadcast

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));
}

Solution

  • 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
    }