How to download multiple files concurrently using intentservice in Android?

亡梦爱人 提交于 2019-11-30 04:33:30

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/

IntenService and AsyncTask provide a single worker thread, therefore simultaneous download of multiple images is not possible. However, i strongly suggest ThreadPoolExecutor for your requirements. It basically creates a pool of threads which are expended/shrink based on the number of tasks you apply or number of files you want to download. THreadPoolExecutor handles practically every aspect of threads management and its quite efficient. You create a single executor with the as in this code sample:

ExecutorService tpExecutor=
        new ThreadPoolExecutor(
                coreSize,      // the basic number of threads, when a task is added to the queue
                               // a new thread is created if nrOfThreads < coreSize  
                maxPoolSize,   // maximum number of threads created
                keepAliveTime,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>()
                ); 

You can submit multiple downloading task by executing runnables:

tpExecutor.execute(new Runnable());

From the sound of it you might benefit from using the onProgressUpdate method from ASyncTask, which can keep you updated on progress. Maybe have a read through the class description -- it sounds like just what you need. It also comes with lots of sample code!

Hope I am of help!

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