Android 应用更新广播

Android app update broadcast

所以,我正在做一些事情,要求我知道设备上的另一个应用程序何时更新。所以我的问题很简单,假设 YouTube 或 Spotify 在应用程序更新时发送广播,如果是这样,我需要用我的 broadcastReceiver 捕捉什么。

ACTION_PACKAGE_REPLACED 是更新应用程序时触发的操作 根据文档:

A new version of an application package has been installed, replacing an existing version that was previously installed. The data contains the name of the package.

intent.getPackage() returns package/application 这个意图是专门针对但它被发送给任何感兴趣的接收者,因此,没有这样的包裹。

使用 intent.getData() 其中 returns 更新包作为 Uri

在 Manifest.xml

中注册广播接收器时添加以下 intent 过滤器
<intent-filter>
   <action android:name="android.intent.action.PACKAGE_REPLACED" />
   <data android:scheme="package" />
</intent-filter>

你的意图过滤器应该是这样的:

<intent-filter>
   <action android:name="android.intent.action.PACKAGE_REPLACED" />
   <data android:scheme="package" />
</intent-filter>

您的 BroadcastReceiver 的 onReceive 方法应该是:

@Override
public void onReceive(Context context, Intent intent) {
    Uri data = intent.getData();
    if (data.toString().equals("package:" + "com.target.package") {
        // action to do
    }
}

According to android docs: Broadcast Action: A new version of your application has been installed over an existing one. This is only sent to the application that was replaced. It does not contain any additional data; to receive it, just use an intent filter for this action.

<intent-filter>
   <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
   <data android:scheme="package" />
</intent-filter>

您的 BroadcastReceiver 的 onReceive 方法应该是:

@Override
public void onReceive(Context context, Intent intent) {
    // action to do

}