Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

android - Differentiate implicit broadcast receiver vs explicit broadcast receiver in the manifest

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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+.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...