我正在尝试使用下载管理器下载文件,并且下载似乎没有在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中引入的更改有什么关系?
好像你已经定义了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"
如果它有效 - 强烈建议不允许所有主机的流量,但仅限于需要。