出于某种原因,谷歌开发人员(名为“Dianne Hackborn”)认为可以单独注册到当前应用程序的 PACKAGE_REPLACED 意图(阅读存档版本)here https://web.archive.org/web/20161206025533/http://osdir.com/ml/Android-Developers/2009-11/msg04736.html, 原文链接here http://osdir.com/ml/Android-Developers/2009-11/msg04736.html).
但是,我找不到任何正确执行此操作的方法,因此我做出了妥协:它将使用可用的最新 API。
可悲的是,我无法找出为什么它无法调试,但它确实有效(如果您愿意,您可以写入日志)。
这是代码:
显现:
<receiver
android:name=".OnUpgradeBroadcastReceiver"
android:enabled="@bool/is_at_most_api_11" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<receiver
android:name=".OnUpgradeBroadcastReceiver"
android:enabled="@bool/is_at_least_api_12" >
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
资源/值/version_checks.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="is_at_least_api_12" type="bool">false</item>
<item name="is_at_most_api_11" type="bool">true</item>
</resources>
res/values-v12/version_checks.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="is_at_least_api_12" type="bool">true</item>
<item name="is_at_most_api_11" type="bool">false</item>
</resources>
OnUpgradeBroadcastReceiver.java
public class OnUpgradeBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
if (VERSION.SDK_INT <= VERSION_CODES.HONEYCOMB
&& !context.getPackageName().equals(intent.getData().getSchemeSpecificPart())) {
android.util.Log.d("AppLog", "other apps were upgraded");
return;
}
android.util.Log.d("AppLog", "current app was upgraded");
编辑:在今天的 Android 版本中,当您应该将 minSdk 设置为至少 14 时,您不需要这个,实际上您应该只使用 MY_PACKAGE_REPLACED 就这样。不需要布尔值等......