出于某种原因,谷歌开发人员(名为“Dianne Hackborn”)认为可以单独注册当前应用程序的 PACKAGE_REPLACED 意图(在此处阅读存档版本,此处为原始链接)。
但是,我找不到任何正确的方法,所以我做出了妥协:它将在可用时使用最新的 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>
res/values/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 就可以了。不需要布尔值等...