我有一个BroadcastReceiver,应该在下载完成后祝酒,如下所示:
class DownloadBroadcastReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE == intent.action) {
Toast.makeText(context, R.string.toast_save_successful, Toast.LENGTH_SHORT).show()
Log.d("DownloadBroadcastReceir", "toasted")
}
}
}
我可以在日志中看到调试消息但是没有出现toast。
这里有什么显而易见的东西吗?
对于上下文,以下是对下载服务的调用:
val downloader = DownloadManager.Request(uri)
downloader.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE or DownloadManager.Request.NETWORK_WIFI)
.setDestinationInExternalPublicDir(destinationLocation)
.setAllowedOverRoaming(true)
.setTitle(filename)
.setDescription(resources.getString(R.string.downloader_description, sourceFilename))
(saverActivity.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager).enqueue(downloader)
此处它已在清单中注册:
<receiver android:name=".receiver.DownloadBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
应用程序通常在下载完成时关闭。这有什么不同吗?