gpt4 book ai didi

android - 区分 list 中的隐式广播接收器与显式广播接收器

转载 作者:IT王子 更新时间:2023-10-28 23:30:02 30 4
gpt4 key购买 nike

根据 Google 提供的 Android O 迁移指南,大部分隐式广播 Intent 不应在 Manifest 中注册(除了发现的一些异常(exception) here ),但显式广播 Intent 保持不变。

我们希望将任何需要的广播从 list 中移出。但是我们如何识别接收者是否是隐式的?有一般规律吗?

这是我们在 list 中注册的广播示例。我们是否应该只查看“action”标签并查看它是否被列入白名单以将其保留在 list 中?

<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” Intent 未列入白名单。我们应该在 Activity 中注册它吗?如果是这样,它不会永远不会被解雇,因为当我们注册它时,应用程序已经安装了?当我试图理解广播接收器是隐式还是显式时,这让我感到困惑,因为我认为我只需要检查那个“ Action ”标签。

最佳答案

But how do we recognise if a receiver is implicit?

如果 Intent 具有 ComponentName,则 Intent 是显式的。否则,它是隐式的。

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

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

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

  • 可以由系统从action string等加上setPackage()定义的包中导出

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

没有。您还需要考虑广播的性质:它会发送给任何注册的接收者,还是只发送给特定的应用程序?

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

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

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

关于android - 区分 list 中的隐式广播接收器与显式广播接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46121467/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com