清单中 DownloadManager.ACTION_DOWNLOAD_COMPLETE 的接收者

Receiver for DownloadManager.ACTION_DOWNLOAD_COMPLETE in Manifest

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

我找到的所有示例都使用 class 中的 registerReceiver(downloadCompleteReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));,但我想在 Manifest.xml 中接收它,这样我可以在应用程序关闭时收听。 当我尝试在 Manifest.xml

中为我的接收器设置 intent-filter 时,我找不到这个动作

正如 official documentation 所述:

Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers.

If your app targets Android 8.0 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that don't target your app specifically). You can still use a context-registered receiver when the user is actively using your app.

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>