Android app update broadcast

谁说我不能喝 提交于 2019-11-29 07:29:18

Your intent filter should be like:

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

And onReceive method from your BroadcastReceiver should be:

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

ACTION_PACKAGE_REPLACED is action triggered when an application is being updated According to docs :

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 the package/application this intent was specifically addressed to but it was sent to any interested receiver, therefore, there isn't such package.

Use intent.getData() which returns the updated package as an Uri

Add the below intent filter while registering a broadcast receiver in your Manifest.xml

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

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>

And onReceive method from your BroadcastReceiver should be:

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

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!