Android 8.0 以前静态注册
在 AndroidManifest.xml 中加入静态注册即可。
<receiver
android:name=".InstallReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Android 8.0 及以后需要动态注册
InstallReceiver installReceiver = new InstallReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addDataScheme("package");
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
registerReceiver(this.installReceiver, intentFilter);
Android 12.0 及以后需要增加权限
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
|