Android Oreo (API26) and android.app.DownloadManager
Asked Answered
P

1

3

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)
}
Pedicure answered 26/12, 2017 at 18:34 Comment(3)
hi, did you figure it out why that was happening?Stereoscopic
Seems like it's a simulator related issue, I found a workaround: request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)Pedicure
oh great! you should post it as an answer - I am going crazy here looking through all api diffs to see if they just forgot to document the changeStereoscopic
P
2

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) 
Pedicure answered 6/1, 2018 at 19:3 Comment(1)
Oooh I found out why! My emulator is set to be running on mobile data instead of wifi: i.imgur.com/OvdDvX8.pngStereoscopic

© 2022 - 2024 — McMap. All rights reserved.