10分钟后,DownloadManager失败

时间:2019-02-19 10:32:21

标签: java android android-download-manager

我目前正在尝试从网络服务(WebApi 2.0 HttpGet method)下载视频。

此文件可能要花一些时间才能下载(最多20分钟或更长时间)。

要下载,我使用本机DownloadManager,它适用于小型(快速)文件。

如果下载过程耗时超过10分钟,则下载将在10日以DOWNLOAD_FAILED状态停止。

首先,我认为这是我的IIS / WebApi配置的问题,但是如果我使用chrome(或任何其他浏览器)下载文件,即使它花费了20分钟或更长的时间也会成功,因此它必须是Client方面的问题。

我在DownloadManager中初始化onCreate obj:

downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

这是我的DownloadManager代码:

private void downloadVideo(final int position) {
    progressDialogLayout.setVisibility(View.VISIBLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

    downloadingVideoId = adapter.getItem(position).getId();
    Uri downloadUri = Uri.parse("MyURL");

    DownloadManager.Request request = new DownloadManager.Request(downloadUri);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    request.setAllowedOverRoaming(false);
    request.setTitle(Objects.requireNonNull(adapter.getItem(position)).getDeadFullName());
    request.setDescription("Download in corso...");
    request.setVisibleInDownloadsUi(true);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MOVIES, "/" + adapter.getItem(position).getId() + ".mp4");

    refid = downloadManager.enqueue(request);

    new Thread(new Runnable() {
        @Override
        public void run() {
            boolean downloading = true;
            while (downloading) {
                if (refid == null)
                    return;
                DownloadManager.Query q = new DownloadManager.Query();
                q.setFilterById(refid);
                Cursor cursor = downloadManager.query(q);
                cursor.moveToFirst();
                int bytes_downloaded = cursor.getInt(cursor
                  .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

                if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                    downloading = false;
                }

                final double dl_progress = (int) ((bytes_downloaded * 100L) / (bytes_total == 0 ? 1 : bytes_total));

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        pbDialogProgress.setProgress((int) dl_progress);
                        tvDialogProgressPercentage.setText(String.format(Locale.getDefault(), "%d%%", (int) dl_progress));
                    }
                });

                Log.d("DOWNLOAD", DownloadManagerUtils.StatusMessage(cursor)); //it just parses the status into a readable string
                cursor.close();
            }

        }
    }).start();
}

也就是说,为什么我的下载管理器恰好在10分钟后停止?没有超时配置,所以我无法弄清楚出了什么问题。

谢谢

0 个答案:

没有答案