DownloadManager在开始下载时有很大的延迟

时间:2019-06-17 16:04:12

标签: android android-download-manager download-manager

我正在使用此源代码在android的公共和外部“下载”目录中下载文件。

function wordCount(str) {
  str = str || 'I came **home**. And while at **home** I did this and that.';
  var countObj = str
.toUpperCase() //'I CAME **HOME**...'
.replace(/[^A-Z ]/g, '') //'I CAME HOME...'
.split(' ') //['I', 'CAME',..]
.reduce(function(obj, word) {
  if (word.length >= 3) {
    obj[word] = obj[word] ? ++obj[word] : 1;
  }
  return obj;
}, {}); //{HOME:2,DID:1}

  return Object.keys(countObj)
.map(function(word) {
  return [word, countObj[word]];
}) //[['HOME',2],['CAME',1],...]
.sort(function(a, b) {
  return b[1] - a[1];
});
}
console.info(wordCount());

当url为http://mobimento.com/~psaez/video2.zip时,它会在logcat上显示此错误

private void download(String url){
    if (ActivityCompat.checkSelfPermission(SectionManager.getInstance().getCurrentActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        PermissionManager.getInstance().requestPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, PermissionManager.PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
    } else {
        DownloadManager.Request r = new DownloadManager.Request(Uri.parse(url));

        // This put the download in the same Download dir the browser uses
        //r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, url.substring(url.lastIndexOf('/')+1));
        r.setDestinationInExternalFilesDir(SectionManager.getInstance().getCurrentActivity(), Environment.DIRECTORY_DOWNLOADS, url.substring(url.lastIndexOf('/')+1));

        // When downloading music and videos they will be listed in the player
        // (Seems to be available since Honeycomb only)
        r.allowScanningByMediaScanner();

        // Notify user when download is completed
        // (Seems to be available since Honeycomb only)
        r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        // Start download
        DownloadManager dm = (DownloadManager) SectionManager.getInstance().getCurrentActivity().getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(r);
    }
}

但是在30到40秒后,有时是1或2分钟后,下载会正确开始并完成:

2019-06-17 19:15:01.916 11623-19923/? D/DownloadManager: [191] Starting
2019-06-17 19:15:02.308 11623-19923/? W/DownloadManager: [191] Stop requested with status HTTP_DATA_ERROR: Failed reading response: java.net.ProtocolException: unexpected end of stream
2019-06-17 19:15:02.310 11623-19923/? D/DownloadManager: [191] Finished with status WAITING_TO_RETRY

代码有什么问题?为什么会出现错误,并在30-40秒后开始下载?

1 个答案:

答案 0 :(得分:0)

尝试这些解决方案,并确保您的URL有效

  

解决方案1)

在应用程序标记中添加android:networkSecurityConfig="@xml/network_security_config"

<application
        android:name=".ApplicationClass"
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

其中network_security_config.xml

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

xml目录下创建res文件夹,然后在xml文件夹中创建network_security_config.xml

  

解决方案2)

android:usesCleartextTraffic="true"

将此标签添加到application文件的manifest标签中

<application
        android:name=".ApplicationClass"
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:usesCleartextTraffic="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

参考:- Download Manger not working in Android Pie 9.0 (Xiaomi mi A2) a2