如何下载多个文件使用并发intentservice在Android中? [英] How to download multiple files concurrently using intentservice in Android?
问题描述
我要创建<一个类似这样的一个服务,(参考href="http://stackoverflow.com/questions/19875838/download-multiple-files-with-a-progress-bar-in-listview-android">Here),下载多个文件异步的Android系统。
公共静态类DownloadingService扩展IntentService {
公共静态字符串PROGRESS_UPDATE_ACTION = DownloadingService.class
.getName()+.newDownloadTask;
私人的ExecutorService MEXEC;
私人CompletionService&LT; NoResultType&GT; MECS;
私人LocalBroadcastManager mBroadcastManager;
私人列表&LT; DownloadTask&GT; mTasks;
公共DownloadingService(){
超级(DownloadingService);
MEXEC = Executors.newFixedThreadPool(3); //使用多个线程的原因是异步下载文件。
MECS =新ExecutorCompletionService&LT; NoResultType&GT;(MEXEC);
}
@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
返回super.onStartCommand(意向,标志,startId);
}
@覆盖
保护无效onHandleIntent(意向意图){
而(真)
{
如果(光标&LT; = totalDownloadQueue.size() - 1){// totalDownloadQueue是一个静态的ArrayList,其中包含大量的DownloadTask
mEcs.submit(totalDownloadQueue.get(光标));
光标++; //变量光标也是一个静态的int变量。
}
} //连续观察totalDownloadQueue。如果有任何下载的项目将被添加。那么线程分派来处理。
mExec.shutdown();
}
用户可以选择其中在不同的片段列表视图
下载项目。我的策略是,当用户选择项目和preSS下载按钮,这些项目传递到 DownloadTask
负责下载文件。然后下载任务添加到 totalDownloadQueue
。
这里有一些问题:
-
我知道
intentservice
是由一些定义的动作触发。但我想是创建一个后台服务看totalDownloadQueue
,如果有新的downloadtask
是速效,然后一些线程被称为操作任务。什么是如果我这样做是为这个定制的副作用
intentservice
?我应该使用什么样的替代类?请提供
样品code
伴随着解释,谢谢。 -
据我所知,该线程的初始化仅被一次。如果我启动服务在应用的开始和线程应该被杀死在用户终止该应用程序。(我的意思是,当他
刷出
的窗口。)执行用户退出后线程存在的应用程序? -
如果这个方法仍然不能解决有关异步下载文件的问题?我应该采取什么样的其他策略? 请提供一些例如code或引用,这样我可以修改它。
我还了7天处理复杂的需求,任何帮助,请!
下载多个文件异步的Android系统。
和还我想你想同时下载。
我觉得你滥用了 intentservice
。 intentservice
有一个尺蠖
和处理
键,每次调用开始,那么创建的处理程序的消息。所有的消息都排队在尺蠖队列
,并同时供应之一。
您应该用正常的服务,并且不使用 intentservice
因为要同时而不是一个下载的时间。扩大服务类和的onCreate
方法,你可以创建多个线程,每个线程都可以从 onStartCommand
A的消息。我不想复制并粘贴文档的例子,因为我觉得这是更好地阅读所有的文档的一次。如果你读它,你可以完全理解如何创建能够同时处理多个任务,虽然它在示例中创建一个线程服务。
http://developer.android.com/guide/components/services.html
我想是创建一个后台服务观看 totalDownloadQueue
我觉得你并不需要的。只是当你创建 downloadtask
呼叫服务,您的的消息
交付给服务类,并在类中,你可以创建的BlockingQueue
按线程来处理邮件
。
执行用户退出后,线程存在的应用程序?
是的,也许没有。它依赖于过程中,如果过程中存在肯定的,但如果处理已被破坏没有。重读周期
过程中了解什么进程被杀死或备存的Android系统。
http://developer.android.com/guide/components/流程 - 和 - threads.html
如果这个方法仍然不能解决有关下载文件的问题 异步?我应该采取什么样的其他策略?请提供 一些例如code或引用,这样我可以修改它。
您可以使用下载管理器
但顺序下载。
http://developer.android.com/reference/android/app/ DownloadManager.html
http://blog.vogella.com/2011/06 / 14 / Android的下载管理器,例如/
I want to create a service similar to this one, (reference from Here), to download multiple files asynchronously in Android.
public static class DownloadingService extends IntentService {
public static String PROGRESS_UPDATE_ACTION = DownloadingService.class
.getName() + ".newDownloadTask";
private ExecutorService mExec;
private CompletionService<NoResultType> mEcs;
private LocalBroadcastManager mBroadcastManager;
private List<DownloadTask> mTasks;
public DownloadingService() {
super("DownloadingService");
mExec = Executors.newFixedThreadPool( 3 ); // The reason to use multiple thread is to download files asynchronously.
mEcs = new ExecutorCompletionService<NoResultType>(mExec);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
while(true)
{
if( cursor <= totalDownloadQueue.size() -1) { //totalDownloadQueue is a static ArrayList which contains numerous DownloadTask
mEcs.submit(totalDownloadQueue.get(cursor));
cursor++; // The variable cursor is also a static int variable.
}
}// continuously observing the totalDownloadQueue. If any download item is added. Then the thread are dispatched to handle that.
mExec.shutdown();
}
The user can select download items among listview
in different fragments. My strategy is that as the user select the items and press download button, these items are passed into DownloadTask
which is responsible for downloading a file. Then the download tasks are added into the totalDownloadQueue
.
Here are some questions:
I know the
intentservice
is triggered by some defined action. But what I want to is to create a background service watching thetotalDownloadQueue
, if any newdownloadtask
is availabe, then some thread are called to operate the tasks.What's the side effect if I did so for this customized
intentservice
?What alternative class should I use? Please provide
sample code
along with the explanation, thanks.As I know, the initialization of the threads is only called by once. If I start the service at the beginning of the application and the threads should be killed as the user terminate the app.(I mean when he
swipe out
the window.) Do the threads exist after the user exit the application?If this approach still can't resolve the issue about downloading files asynchronously? What other strategy should I adopt? Please provide some example code or reference so that I can modify on it.
I have spent 7 days dealing with the complex requirement, any help please!
to download multiple files asynchronously in Android.
and also i think you want to download simultaneously.
i think you misused the intentservice
. intentservice
has a looper
and a handler
and each call to start causes to create a message for the handler. all messages are queued in the looper queue
and are served one at a time.
you should use normal service and do not use intentservice
because you want to download simultaneously not one at a time. extend service class and in onCreate
method you can create multiple threads and each thread can take a messages from onStartCommand
. i do not want to copy and paste the doc example because i think it is better to read all of the doc again. if you read it you can completely understand how to create service that handles multiple task simultaneously although it has created just one thread in the example.
http://developer.android.com/guide/components/services.html
what I want to is to create a background service watching the totalDownloadQueue
i think you do not need that. just when you create downloadtask
call service, your message
is delivered to service class and in that class you can create blockingqueue
to handle your messages by threads
.
Do the threads exist after the user exit the application?
yes and maybe no. it depends on the process, if the process exists yes but if the process has been destroyed no. again read lifecycle
of process to understand what process is killed or kept by android.
http://developer.android.com/guide/components/processes-and-threads.html
if this approach still can't resolve the issue about downloading files asynchronously? What other strategy should I adopt? Please provide some example code or reference so that I can modify on it.
you can use downloadmanager
but it downloads sequentially.
http://developer.android.com/reference/android/app/DownloadManager.html
http://blog.vogella.com/2011/06/14/android-downloadmanager-example/
这篇关于如何下载多个文件使用并发intentservice在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!