Control the download ordering of Download manager in android
Asked Answered
B

1

9

have an use case like the following:

There are several files to download e.g. A B C D E F

When the downloading is started , say the A B is finished and C is downloading, I would like to interrupt the download of C and start the download of E

Then, after E is finished (if there is no other interruption), continue to C D F.

So far form my research there is only cancel method

downloadManager.remove(downloadReference); How to achieve this through Download manager or are there other approach ? thanks

    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;
}
Brookebrooker answered 22/10, 2013 at 4:26 Comment(0)
I
7

Regarding this answer, it looks like you can cancel the download and then download the rest of the file. For example:

Register a BrodcastReciever to notify you when C is completed:

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
Immunology answered 24/11, 2013 at 3:29 Comment(3)
Thanks. How to keep the file when cancel the download manager task ? Since remove the refence id in the queue will cause removing the fileBrookebrooker
In fact this code is only work for the case that use I/O stream to download file , but not download manager. As there is no "pause" (stop and keep the file) function in download manager. Are there any work around? ThanksBrookebrooker
I don't know, I'd have to look into that.Immunology

© 2022 - 2024 — McMap. All rights reserved.