Android download queue using DownloadManger

倾然丶 夕夏残阳落幕 提交于 2019-11-30 23:17:55

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:-)

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

@Override
public void onReceive(Context context, Intent intent) {
    long downloadId = 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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!