android下载管理器将同一文件下载两次

1l5u6lss  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(416)

我正在使用android下载管理器下载一个大的zip文件。下载管理器工作正常。但它在一个请求中下载同一个文件两次,并给出两个不同的下载ID。
我不知道它为什么这样做。我只是想该文件应该只下载一次。如果失败,用户可以手动重新下载,失败时也不会自动重新下载。
下面是我的代码:

DownloadManager.Request mediaDownloadRequests = new DownloadManager.Request(Uri.parse(sDownloadableFileURL))
                        .setTitle(mContext.getResources().getString(R.string.app_name))// Title of the Download Notification
                        .setDescription(sDownloadingMsg)// Description of the Download Notification
                        .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)// Visibility of the download Notification
                        .setDestinationUri(Uri.fromFile(zip_mmpk_file)) // Uri of the destination file
                        .setAllowedOverMetered(true)// Set if download is allowed on Mobile network
                        .setAllowedOverRoaming(true)// Set if download is allowed on roaming network
                        .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                downloadManager= (DownloadManager) mContext.getSystemService(mContext.DOWNLOAD_SERVICE);
                long downloadID = downloadManager.enqueue(mediaDownloadRequests));
    Log.e("Downloading For --> ", "URL : "+sDownloadableFileURL + " || Download ID : "downloadID);

我的广播接收机如下:

private BroadcastReceiver onMediasDownloadComplete = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Fetching the download id received with the broadcast
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            DownloadManager.Query q = new DownloadManager.Query();
            q.setFilterById(id);
            Cursor c = downloadManager.query(q);
            if (c.moveToFirst()) {
                if (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                    String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                    performOnDownloadSuccessOperation(id, uriString);
                } else {
                    performOnDownloadFailedOperation(id);
                }
            }
        }
    };

我正在注册我的下载广播接收器
oncreateview()
片段的方法如下所示:

mContext.registerReceiver(onMediasDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

以及在
ondestroyview()
与以下相同片段的方法:

mContext.unregisterReceiver(onMediasDownloadComplete);

有人能告诉我为什么下载管理器要下载同一个文件两次吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题