各位!
此代码不适用于 Android Oreo(但在旧版本上可以,我可以看到通知和DownloadManager.ACTION_DOWNLOAD_COMPLETE
广播消息)。
Kotlin
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.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)