看到这个:
如何知道我的 Android 应用程序已升级以重置警报?
正确的解决方法是您在清单中使用了错误的字符串:http:
//developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED
它应该是“android.intent.action.PACKAGE_REPLACED”。
好的,我看到我写的内容仍然不足以尝试,所以我将破例并发布整个项目以证明它有效:应用程序代码位于名为“com.broadcast_receiver_test”的包中。不要忘记在测试之前运行它,否则它将无法在某些 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