Android Oreo (API26) and android.app.DownloadManager

妖精的绣舞 提交于 2019-12-10 19:28:27

问题


Folks! This code doesn't work on Android Oreo (but ok on older versions, I can see notifications and the DownloadManager.ACTION_DOWNLOAD_COMPLETE broadcast message).

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)
}

回答1:


I found that the API 26 emulators simulate network requests over mobile data, so the simplest workaround is adding the NETWORK_MOBILE flag (at least for debugging the DownloadManager):

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


来源:https://stackoverflow.com/questions/47982409/android-oreo-api26-and-android-app-downloadmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!