见此:
correct fix is that you use the wrong string in the 显示 :
http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED
应该是"和机器人. intent. action. Package_replaced"代替。
ok , i see that what i ve written is still not enough to try it out, so i will make an exception and publish a whole project just to show that it works:
app code is in a package called "com.broadcast_receiver_test" .
don t forget to run it before testing , or else it won t work on some android versions (i think 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>
我的播报Repeiver. 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();
}
}
请运行它,看它是否完美。
EDIT:如果你的应用程序是用于 API12 和以上, 并且只希望处理更新应用程序的个案, 您可以单独使用这个意图 :
< a href=>http:// developmenter.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED" rel=“noreferr'>http://developinger.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED