有一个如下用例:
有几个要下载的文件,例如A B C D E F
当下载开始时,说A B已经完成并且C正在下载,我想中断C的下载并开始下载E
然后,在E完成后(如果没有其他中断),继续进行C D F。
到目前为止,我的研究只有取消方法
downloadManager.remove(downloadReference); 如何通过下载管理器实现这一目标还是有其他方法?感谢
private long startDownload(String url) {
Uri DownloadUri = Uri.parse(url);
String fileName = StorageUtils.getFileNameFromUrl(url);
String destination = null;
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
DownloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle(fileName);
request.setDescription("com.example.services");
if (StorageUtils.isSDCardPresent()
&& StorageUtils.isSdCardWrittenable()
&& StorageUtils.checkAvailableStorage()) {
destination = StorageUtils.SDCARD_ROOT;
}
try {
StorageUtils.mkdir();
} catch (IOException e) {
e.printStackTrace();
}
request.setDestinationInExternalPublicDir(destination, fileName);
downloadReference = downloadManager.enqueue(request);
Log.d("Downloader","Start download manager: " + destination + fileName);
return downloadReference;
}
答案 0 :(得分:7)
关于this回答,看起来您可以取消下载,然后下载文件的其余部分。例如:
注册BrodcastReciever以在C完成时通知您:
BroadcastReceiver onComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//check if it is B that is complete
//cancel C
// download E
//check if it is E that is complete
// Open connection to URL.
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
// Specify what portion of file to download.
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
// here "downloaded" is the data length already previously downloaded.
// Connect to server.
connection.connect();
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
//download A
//download B
//download C