相关的Intent:
Intent.ACTION_PACKAGE_INSTALL
Intent.ACTION_PACKAGE_ADDED
Intent.ACTION_PACKAGE_REPLACED
Intent.ACTION_PACKAGE_REMOVED
Intent.ACTION_PACKAGE_CHANGED
Intent.ACTION_PACKAGE_RESTARTED
Intent.ACTION_PACKAGE_DATA_CLEARED
AndroidManifest.xml中配置
<receiver android:name=".MyInstalledReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"></action>
<action android:name="android.intent.action.PACKAGE_CHANGED"></action>
<action android:name="android.intent.action.PACKAGE_REMOVED"></action>
<action android:name="android.intent.action.PACKAGE_REPLACED"></action>
<action android:name="android.intent.action.PACKAGE_RESTARTED"></action>
<action android:name="android.intent.action.PACKAGE_INSTALL"></action>
<data android:scheme="package"></data>
</intent-filter>
</receiver>
或者在程序中动态注册
receiver = new PackageBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
filter.addDataScheme("package");
registerReceiver(receiver, filter);
示例代码
public class MyInstalledReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();
} else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();
}
}
}