Differentiate implicit broadcast receiver vs explicit broadcast receiver in the manifest
Asked Answered
F

1

29

According to the migration guide to Android O given by Google, most of the implicit broadcast intent should not be registered in the Manifest (minus a few exceptions found here) but explicit broadcast intents remain untouched.

We are looking to move any needed broadcast away from the manifest. But how do we recognise if a receiver is implicit? Is there a general rule?

Here is a sample of the broadcasts we register in the manifest. Should we look only at the "action" tag and see if it is whitelisted to keep it in the manifest?

<receiver
    android:name=".receiver.ImageBroadcastReceiver"
    android:enabled="true" >
    <intent-filter>
        <action android:name="android.hardware.action.NEW_PICTURE" />
        <category android:name="android.intent.category.OPENABLE" />
        <data android:mimeType="image/*" />
    </intent-filter>
</receiver>

<receiver
    android:name=".receiver.InstallReferrerReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
</receiver>

<receiver android:name=".receiver.JoinEventReceiver" >
    <intent-filter>
        <action android:name="JOIN_ACTION" />
        <action android:name="CANCEL_ACTION" />
        <action android:name="DECLINE_ACTION" />
    </intent-filter>
</receiver>

For example, the "com.android.vending.INSTALL_REFERRER" intent is not whitelisted. Should we register it in an Activity? If so wouldn't it be never fired as when we register it the app is already installed? This is what confuses me when trying to understand if a broadcast receiver is implicit or explicit as I thought I only had to check that "action" tag.

Fajardo answered 8/9, 2017 at 17:7 Comment(0)
G
45

But how do we recognise if a receiver is implicit?

If the Intent has a ComponentName, the Intent is explicit. Otherwise, it is implicit.

That ComponentName can be obtained in one of a few ways, including:

  • It can be directly put on the Intent (e.g., new Intent(this, TheReallyAwesomeReceiver.class)

  • It can be directly put on the Intent after using PackageManager and queryIntentReceivers() to find the right one based on action strings, etc.

  • It can be derived by the system from the action string, etc. plus the package defined via setPackage()

Should we look only at the "action" tag and see if it is whitelisted to keep it in the manifest?

No. You also need to think about the nature of the broadcast: is it going to any registered receiver, or only to a specific app?

For example, the "com.android.vending.INSTALL_REFERRER" intent is not whitelisted. Should we register it in an Activity?

No. That broadcast will only go to the app that was recently installed, and so it must be an explicit Intent. The action string and such are there to help the system determine which of your registered receivers is the relevant one.

Contrast that with ACTION_PACKAGE_ADDED. That is broadcast to any registered receiver; it is not going to just one specific app. Hence, that Intent must be implicit (as otherwise it would have a ComponentName identifying a specific receiver in a specific app). And, since ACTION_PACKAGE_ADDED is not on the whitelist, the assumption should be that you cannot register for this broadcast in the manifest on Android 8.0+.

Gunpowder answered 8/9, 2017 at 17:13 Comment(5)
setClassName is another option, right? Are there more?Billy
@FlorianWalther: setComponent(). There may be others; I do not have them all memorized.Gunpowder
You posted about a workaround that uses PackageManager on your blog over 1 year ago. Have you experienced any trouble with it so far? Is it a full replacement for static implicit receivers? commonsware.com/blog/2017/04/11/…Billy
@FlorianWalther: I am not aware of any problems with the technique, though I haven't had a need to use it personally.Gunpowder
See this related question for more info including some code: #46305339Shammer

© 2022 - 2024 — McMap. All rights reserved.