繁体   English   中英

Android Oreo(API26)和android.app.DownloadManager

[英]Android Oreo (API26) and android.app.DownloadManager

伙计们! 此代码在Android Oreo上不起作用(但在旧版本上可以,我可以看到通知和DownloadManager.ACTION_DOWNLOAD_COMPLETE广播消息)。

科特林

testButton.setOnClickListener {
    val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
    val uri = Uri.parse("[url for a mp3 file]")
    val request = DownloadManager.Request(uri)

    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
    request.setAllowedOverRoaming(false)
    request.setTitle("Test mp3")
    request.setDescription("Wow!")
    request.setVisibleInDownloadsUi(true)
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/GadgetSaint/"  + "/" + "Sample" + ".mp3")

    val reference = downloadManager.enqueue(request)
}

我发现API 26仿真器通过移动数据模拟网络请求,因此最简单的解决方法是添加NETWORK_MOBILE标志(至少用于调试DownloadManager ):

request.setAllowedNetworkTypes(DownloadManager.Request.NETWO‌​RK_WIFI | DownloadManager.Request.NETWORK_MOBILE) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM