有一个像下面这样的用例:
有几个文件可以下载,例如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;
}
最佳答案
关于 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
关于android - 控制android中下载管理器的下载顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508960/