これを参照してください:
アラームをリセットするために Android アプリケーションがアップグレードされたことを確認するにはどうすればよいですか?
正しい修正は、マニフェストで間違った文字列を使用することです:
http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED
代わりに「android.intent.action.PACKAGE_REPLACED」にする必要があります。
わかりました、私が書いたものはまだ試してみるのに十分ではないことがわかりました. テストする前に実行することを忘れないでください。そうしないと、一部の Android バージョン (API 11+ だと思います) では動作しません。
マニフェスト:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcast_receiver_test" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity android:name=".BroadcastReceiverTestActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver">
<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_REMOVED"/>
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>
</manifest>
MyBroadcastReceiver.java:
public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(final Context context,final Intent intent)
{
final String msg="intent:"+intent+" action:"+intent.getAction();
Log.d("DEBUG",msg);
Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
}
}
実行して、完全に動作することを確認してください。
編集: アプリが API12 以降用であり、アプリの更新のみを処理したい場合は、このインテントを単独で使用できます。
http://developer.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED