Receiving package install and uninstall events
Other answers point out listening for ACTION_PACKAGE_ADDED
and ACTION_PACKAGE_REPLACED
broadcasts. That is fine for Android 7.1 and lower. On Android 8.0+, you cannot register for those broadcasts in the manifest.
Instead, you need to call getChangedPackages()
on PackageManager
periodically, such as via a periodic JobScheduler
job. This will not give you real-time results, but real-time results are no longer an option on Android 8.0+.
I tried to register the BroadcastReceiver
in either manifest file or java code. But both of these two methods failed to trigger the onReceive()
method.
After googling this problem, I found a solution for both methods from another Thread in SO:
Android Notification App
In the manifest file (this approach no longer applies since API 26 (Android 8), it was causing performance issues on earlier Android versions):
<receiver android:name=".YourReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
In java code:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
intentFilter.addDataScheme("package");
registerReceiver(br, intentFilter);
This should work for you.