安卓下载队列使用DownloadManager

4

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 in queue one after another and as far as i know DownloadManager doesn't provide this functionality. So multiple call to DownloadManager.enqueue(...) results in concurrent download of all the files. How can i fix this?

I can not just make a queue in my activity and send downloads to DownloadManger one by one since activity may be destroyed at any time!

Also IntentService doesn't work here!! even though it handles request one by one, call to DownloadManager.enqueue() will run so fast and then the next call and the result would be concurrent download again!

My third option is to use LocalService that gets the request and calls DownloadManager.enqueue() when the previously started download is finished but how should i do it? my service needs to get request form my activity even when its running! (so i can't just put data in intent). To enable communication i need to make it a bound service and as documentations says it destroys when there is nothing bind to it!

bound service runs only as long as another application component is bound to it. 
Multiple components can bind to the service at once, but when all
of them unbind, the service is destroyed.

So i lose my downloads that are in queue when my activity is closed. Am i right?

And there is final option which is using a service in separate process because even if my third option works it only downloads files as long as application is not closed. this option seems to be the scary one since i have to handle interprocess communication and i have no idea what that is!!

So am i missing something?! shouldn't it be an easier solution to my problem?

I just what to download files is queue! I also don't want my service to run indefinitely when there is nothing to download.

2个回答

5

非常简单:

1)创建一个数据库并将您的url插入其中。

2)在您的清单文件中为downloadmanager的下载完成操作创建一个接收器。

3)当收到下载完成时,从您的数据库中读取一行并开始新的下载(enqueue)。

祝编码愉快:-)


我已经完成了所有这些!我认为你没有正确理解我的问题。我知道何时下载完成并可以开始新的下载。问题是,如果我只按照你说的去做,应用程序将在关闭或甚至进入后台后无法继续下载队列中的其他下载。 - Alireza Ahmadi
我认为你可能是对的!但我不确定我想这样做。因为每当有东西被下载时,我的应用程序都会收到通知,而99%的时间它与我的应用程序无关。无论如何,我找到了使用IntentService解决此问题的方法。请看我的回答。 - Alireza Ahmadi
如何在我的主屏幕上更新这些下载的进度?感谢您的帮助。提前致谢。 - Nabeel K

3
我找到了答案! IntentService的问题在于它执行DownloadManager.engueue()非常快,导致所有下载都同时进行。因此,我通过在后台线程上调用wait()并在广播onReceive被调用且我的下载完成时调用notify()来确保请求在下载完成之前不会完成运行!
我在下载文件后运行了这个。
    currentDownloadId = downloadManager.enqueue(downloadRequest);
    backgroundThread = Thread.currentThread();
    synchronized (Thread.currentThread()) {
        try {
            Thread.currentThread().wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

当下载完成后,我运行以下代码:

@Override
public void onReceive(Context context, Intent intent) {
    long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,0);
    if (downloadId == currentDownloadId) {
        synchronized (backgroundThread) {
            backgroundThread.notify();
        }
    }
}

现在当前下载的执行已经完成并自动开始下一个下载!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接