9

有一个如下用例:

有几个文件可供下载,例如 ABCDEF

开始下载,说AB已经完成,C正在下载,我想中断C的​​下载,开始E的下载

然后,E结束后(如果没有其他中断),继续CD F。

到目前为止,我的研究只有取消方法

downloadManager.remove(下载参考); 如何通过下载管理器或有其他方法来实现这一点?谢谢

    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;
}
4

1 回答 1

7

关于这个答案,您似乎可以取消下载,然后下载文件的其余部分。例如:

注册 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
于 2013-11-24T03:29:46.487 回答