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

查看:261
本文介绍了Android Oreo(API26)和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标志(至少用于调试):

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

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

解决方案

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) 

这篇关于Android Oreo(API26)和android.app.DownloadManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆