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

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

问题描述

根据Google提供的Android O迁移指南,大多数隐式广播意图都不应在清单中注册(减去一些例外情况

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>

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

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

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天全站免登陆