android.intent.action.DOWNLOAD_COMPLETE是明确广播吗? [英] Is android.intent.action.DOWNLOAD_COMPLETE an explicit broadcast?

本文介绍了android.intent.action.DOWNLOAD_COMPLETE是明确广播吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序(targetSdk = 25)在清单中定义了一个广播接收器,如下所示:

My app (targetSdk=25) has a broadcast receiver defined in the manifest as follows:

<receiver android:name="my.package.DownloadManagerReceiver"
     android:exported="true">
     <intent-filter>
          <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
     </intent-filter>
</receiver>

每当Android的 DownloadManager 完成下载文件时,我的DownloadManagerReceiver就会收到通知,因此我可以对下载的文件进行一些处理.

My DownloadManagerReceiver is notified whenever Android's DownloadManager finishes downloading a file, so I can do some processing on the file that was downloaded.

我正在将应用程序的targetSdk迁移到27(奥利奥).根据 https://developer.android.com/about/versions/oreo/background通过清单注册的#broadcasts 隐式广播接收器不应在Android O中使用(除了列入白名单的例外).

I'm working on migrating my app's targetSdk to 27 (Oreo). According to https://developer.android.com/about/versions/oreo/background#broadcasts, implicit broadcast receivers registered through the manifest are not supposed to work in Android O (except for those whitelisted exceptions).

但是,当我使用运行Android 8.0和targetSdk = 27的仿真器运行我的应用程序时,下载完成后,DownloadManager仍会通知我在清单中定义的广播接收器.

However, when I run my app using an emulator running Android 8.0 and targetSdk=27 my broadcast receiver defined in the manifest is still notified by the DownloadManager after a download completes.

我试图找到DownloadManager发送广播的源代码,以了解其如何发送广播,但找不到.

I tried to find the source code where the DownloadManager sends its broadcast to understand how it sends its broadcasts but I couldn't find it.

有人知道android.intent.action.DOWNLOAD_COMPLETE是显式广播而不是隐式广播吗?为什么我的接收器仍在接收该广播的任何想法?

Does anybody know whether android.intent.action.DOWNLOAD_COMPLETE is an explicit broadcast rather than an implicit one? Any ideas why my receiver is still receiving that broadcast?

推荐答案

在深入研究平台的源代码后,我发现了以下内容:

Here's what I found after digging further into the platform's source code:

1)实例化DownloadManager时,它会保留对应用程序上下文的引用,并从该上下文中提取应用程序的程序包名称

1) When we instantiate the DownloadManager, it keeps a reference to the context of the app and extracts the app's package name from that context

2)当请求下载时,该软件包名称将被插入下载数据库的Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE列中

2) That package name is inserted into the downloads database into column Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE when the download is requested

3)下载完成后, DownloadInfo.sendIntentIfRequested()方法将调用 Intent.setPackage()传递程序包名称.根据方法Intent.setPackage()的描述:

3) When the download completes, the DownloadInfo.sendIntentIfRequested() method will call Intent.setPackage() passing the package name. According to the description of method Intent.setPackage():

(通常为可选),设置一个明确的应用程序包名称,该名称应 限制此Intent将解决的组件.如果留给 默认值为null,所有应用程序中的所有组件将 经过考虑的.如果非空,则Intent只能与以下组件匹配 给定的应用程序包.

(Usually optional) Set an explicit application package name that limits the components this Intent will resolve to. If left to the default value of null, all components in all applications will considered. If non-null, the Intent can only match the components in the given application package.

基于该描述,我的理解是广播意图将针对我的应用,因此可以作为明确意图.

Based on that description, my understanding is that the broadcast intent will be targeted at my app, thus working as an explicit intent.

这篇关于android.intent.action.DOWNLOAD_COMPLETE是明确广播吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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