阻止 android.intent.action.PACKAGE_REMOVED 更新包

Prevent android.intent.action.PACKAGE_REMOVED on Package Update

我在 Manifest 中注册了全局 Broadcast Receiver,它会在用户卸载软件包时显示通知。

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

问题是当某些包从 Play 商店更新时 android.intent.action.PACKAGE_REMOVEDandroid.intent.action.PACKAGE_ADDED相继调用。

问题是我无法区分软件包是卸载还是更新。

一种方法是等到我们收到相同包名的PACKAGE_ADDED,然后关闭通知。

还有其他正确的方法可以实现吗?

我自己搞定了!通过查看收到的包名是否安装来区分!

您还可以检查 EXTRA_REPLACING 额外的意图(如果它是 PACKAGE_REMOVED 意图)。

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.