Skip to content Skip to sidebar Skip to footer

Android Download Queue Using Downloadmanger

I'm using DownloadManager to download my files in android and its great since it handles everything (connectivity lost, retry, etc.) The problem is I want my file to be downloaded

Solution 1:

Very simple:

1)Create a database and insert your url into it.

2)create a receiver for download complete action of downloadmanager in your manifest.

3)when received a download complete read a row from your database and start new download(enqueue).

happy coding:-)

Solution 2:

I found the answer! The problem with IntentService is it execute DownloadManager.engueue() so fast and as a result all download get downloaded together. So i made sure requests are not finished running until download is finished simply by calling wait() on background thread. and calling notify() when Broadcast onReceive is called and my download is finished!

I run this after downloding the file

    currentDownloadId = downloadManager.enqueue(downloadRequest);
    backgroundThread = Thread.currentThread();
    synchronized (Thread.currentThread()) {
        try {
            Thread.currentThread().wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

and when download is finished i run this

@OverridepublicvoidonReceive(Context context, Intent intent) {
    longdownloadId= intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,0);
    if (downloadId == currentDownloadId) {
        synchronized (backgroundThread) {
            backgroundThread.notify();
        }
    }
}

now execution of current download gets finished and next one begans automatically!

Post a Comment for "Android Download Queue Using Downloadmanger"