何らかの理由で、グーグル開発者(「ダイアンハックボーン」という名前)は、現在のアプリの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を使用するだけで、それだけです。ブール値などは必要ありません...