I am trying to make my receiver that I statically defined in the manifest dynamic by creating it programmatically.
That's the static version in the manifest:
<receiver android:name=".receivers.PackageReceiver" android:enabled="true" 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" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</receiver>
That's how I did it in code:
val intentFilter = IntentFilter()
intentFilter.addAction(ACTION_PACKAGE_ADDED)
intentFilter.addAction(ACTION_PACKAGE_REMOVED)
intentFilter.addCategory(CATEGORY_DEFAULT)
intentFilter.addDataScheme("package")
registerReceiver(PackageReceiver(), intentFilter)
The programmatic version is not triggered when a package is installed/uninstalled whereas the static one is. I figure it's because the first version is exported - how can I achieve this in code?