2

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?

phoebus
  • 1,280
  • 1
  • 16
  • 36
  • 1
    Calling `registerReceiver()` on a `Context` creates an exported receiver -- there is nothing else to be done. Try removing `CATEGORY_DEFAULT`, as we usually do not use a category in a broadcast `Intent` filter. Also, where are you calling this code (activity? service? something else?). – CommonsWare Sep 13 '18 at 20:40

0 Answers0