在清单中区分隐式广播接收器和显式广播接收器 [英] Differentiate implicit broadcast receiver vs explicit broadcast receiver in the manifest

查看:43
本文介绍了在清单中区分隐式广播接收器和显式广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据谷歌给出的Android O迁移指南,大部分隐式广播意图不应该在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?

以下是我们在清单中注册的广播示例.我们是否应该只查看action"标签,看看它是否被列入白名单以将其保留在清单中?

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>

例如,com.android.vending.INSTALL_REFERRER"意图未列入白名单.我们应该在活动中注册它吗?如果是这样,当我们注册应用程序时,它不会永远不会被触发吗?当我试图理解广播接收器是隐式还是显式时,这让我感到困惑,因为我认为我只需要检查动作"标签.

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.

推荐答案

但是我们如何识别接收者是否是隐式的?

But how do we recognise if a receiver is implicit?

如果 Intent 有一个 ComponentName,则 Intent 是显式的.否则,它是隐式的.

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

ComponentName 可以通过以下几种方式之一获得,包括:

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

  • 可以直接放在Intent上(例如,new Intent(this, TheReallyAwesomeReceiver.class)

使用PackageManagerqueryIntentReceivers()后可以直接放到Intent上,根据action找到合适的字符串等

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

可以由系统从动作字符串等加上通过setPackage()

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

我们是否应该只查看action"标签,看看它是否被列入白名单以将其保留在清单中?

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?

例如,com.android.vending.INSTALL_REFERRER"意图未列入白名单.我们应该在活动中注册它吗?

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

没有.该广播只会发送到最近安装的应用程序,因此它必须是显式的 Intent.操作字符串等可帮助系统确定您注册的哪个接收者是相关的.

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.

对比 ACTION_PACKAGE_ADDED.广播给任何注册的接收者;它不只是一个特定的应用程序.因此,该 Intent 必须是隐式的(否则它将有一个 ComponentName 标识特定应用程序中的特定接收器).而且,由于 ACTION_PACKAGE_ADDED 不在白名单中,因此假设您无法在 Android 8.0+ 的清单中注册此广播.

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

这篇关于在清单中区分隐式广播接收器和显式广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆