4

我正在尝试使用下载管理器下载文件,但下载似乎没有在 Android P 上开始(在模拟器上运行)。它在 Android Oreo 和 Android Marshmallow 上运行良好。这是代码。

DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(URLS.URLforDownloadingFileData(fileName)));
        File file = new File(mobileInfo.getExternalFileStorageDirectory(),fileName);
        downloadRequest.setDestinationUri(Uri.fromFile(file));
        downloadRequest.setTitle(titleOfDownload);
        downloadRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        downloadManager.enqueue(downloadRequest);

知道为什么会发生这种情况以及如何解决这个问题吗?与 Android P 中引入的更改有关吗?

4

1 回答 1

4

好像您已经为下载 url 主机定义了NetworkSecurityPolicy 。您可以通过创建 XML 来检查它res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

然后在 AndroidManifest.xml 的 Application 标签中引用这个文件:

android:networkSecurityConfig="@xml/network_security_config"

如果它有效 - 强烈建议不要允许所有主机的流量,而只允许需要的流量。

于 2018-09-12T10:28:33.670 回答