Dhruv Kaushal
Dhruv Kaushal

Reputation: 670

Prevent android.intent.action.PACKAGE_REMOVED on Package Update

I have registered a global Broadcast Receiver in Manifest which shows a notification when the user uninstalls a package.

 <receiver android:name=".YourReceiver">
        <intent-filter android:priority="999">
            <action android:name="android.intent.action.PACKAGE_REMOVED" />
            <action android:name="android.intent.action.PACKAGE_INSTALL" />
            <action android:name="android.intent.action.PACKAGE_REPLACED"/>
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="com.times.REFRESH_INSTALL_FLAG" />

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

The problem is that when some package is updating from Play Store the action android.intent.action.PACKAGE_REMOVED and android.intent.action.PACKAGE_ADDED are called one after another.

The problem is that I am unable to differentiate whether a package is uninstalled or updating.

One of the way is to wait till we receive PACKAGE_ADDED for the same package name and then dismiss a notification.

Is there any other correct method to achieve this?

Upvotes: 3

Views: 5935

Answers (2)

nate
nate

Reputation: 311

You could also check the EXTRA_REPLACING extra on the intent (if it is a PACKAGE_REMOVED intent).

public static final String EXTRA_REPLACING

Used as a boolean extra field in ACTION_PACKAGE_REMOVED intents to indicate that this is a replacement of the package, so this broadcast will immediately be followed by an add broadcast for a different version of the same package.

Upvotes: 3

Dhruv Kaushal
Dhruv Kaushal

Reputation: 670

Got it myself! Distinguish by checking whether the received package name is installed or not!

Upvotes: 1

Related Questions