【问题标题】:Receiver for DownloadManager.ACTION_DOWNLOAD_COMPLETE in Manifest清单中 DownloadManager.ACTION_DOWNLOAD_COMPLETE 的接收器
【发布时间】:2019-08-20 01:22:11
【问题描述】:

是否可以在 Manifest.xml 中监听DownloadManager.ACTION_DOWNLOAD_COMPLETE

我发现的所有示例都使用来自classregisterReceiver(downloadCompleteReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));,但我想在 Manifest.xml 中接收它,这样我就可以在应用程序关闭时收听。 当我尝试在 Manifest.xml 中为接收器设置意图过滤器时,我找不到此操作

【问题讨论】:

  • 它可以工作,但你必须手动输入常量值:android.intent.action.DOWNLOAD_COMPLETE
  • @Pawel 我已经试过了,没找到。
  • 仅仅因为 IDE 不自动完成这个值并不意味着它不会在运行时工作。

标签: android android-manifest android-download-manager


【解决方案1】:

正如official documentation 所说:

从 Android 8.0(API 级别 26)开始,系统对清单声明的接收器施加了额外的限制。

如果您的应用以 Android 8.0 或更高版本为目标,则您不能使用清单为大多数隐式广播(不专门针对您的应用的广播)声明接收器。当用户积极使用您的应用时,您仍然可以使用context-registered receiver

Is android.intent.action.DOWNLOAD_COMPLETE an explicit broadcast? 我们了解到android.intent.action.DOWNLOAD_COMPLETE 似乎是一个显式广播,因此在清单中为其定义<receiver> 应该没有问题,即使它不是自动完成的。所以只需使用android.intent.action.DOWNLOAD_COMPLETE 的操作添加它。

<receiver 
    android:name=".your.DownloadCompleteReceiver"
    android:permission="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS"
    android:exported="true">
  <intent-filter>
     <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
  </intent-filter>
</receiver>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-14
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    相关资源
    最近更新 更多