使用DownloadManger的Android下载队列 [英] Android download queue using DownloadManger

查看:540
本文介绍了使用DownloadManger的Android下载队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DownloadManager在android系统中下载文件,这很棒,因为它可以处理一切(连接丢失,重试等).问题是我希望我的文件一个接一个地下载到我的队列中.知道DownloadManager不提供此功能.因此,多次调用DownloadManager.enqueue(...)导致同时下载所有文件.我怎样才能解决这个问题?

我不能只是在活动中排队,然后将下载内容逐个发送到DownloadManger,因为活动可能随时被破坏!

另外IntentService在这里不起作用!即使它一个接一个地处理请求,对DownloadManager.enqueue()的调用仍将如此快速地运行,然后进行下一个调用,其结果将是再次并发下载!

我的第三个选择是使用LocalService来获取请求,并在先前开始的下载完成时调用DownloadManager.enqueue(),但我该怎么办?我的服务即使在运行时也需要从我的活动中获取请求! (所以我不能只是将数据放到意图中).要启用通信,我需要使其成为绑定服务,并且正如文档所述,当没有任何绑定时,它会被破坏!

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.

推荐答案

我找到了答案! IntentService的问题在于它执行DownloadManager.engueue()的速度如此之快,因此所有下载内容都一起下载.因此,我确保仅在后台线程上调用wait()即可完成下载之前的请求.并在调用Broadcast onReceive并完成下载后调用notify()

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!

这篇关于使用DownloadManger的Android下载队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆