0

I am creating Download Manager in which I want to download multiple files at the same time and update the progress for each download. I used following line to execute AsyncTask

executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,Utility.KEY_DOWNLOAD_PATH);

But it doesn't starts multiple AsyncTask. When I click on next download it stops first and starts new download.

Thanks in Advance

Sathish
  • 33
  • 4
unflagged.destination
  • 1,576
  • 3
  • 19
  • 38
  • I think onProgressUpdate() you can call another AsyncTask.How can you call many asyncTask at atime – DJhon May 22 '14 at 10:32
  • on clicking on listItem i can call asyncTask. But it stops the previous one and starts new task – unflagged.destination May 22 '14 at 10:34
  • please see below link: [http://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible][1] [1]: http://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible – MohdTausif May 22 '14 at 10:35
  • 1
    Show code where you start asyncTask. –  May 22 '14 at 10:36
  • Just go here http://developer.android.com/training/multiple-threads/index.html –  May 22 '14 at 10:38
  • I starts asyncTask from listItemClicklistener. // Click event for single list row list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Toast.makeText(activity, "List Item Clicked", Toast.LENGTH_LONG) .show(); fileDownloader.executeOnExecutor( AsyncTask.THREAD_POOL_EXECUTOR, Utility.KEY_DOWNLOAD_PATH); – unflagged.destination May 22 '14 at 10:38
  • Take a hint from this http://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible – DJhon May 22 '14 at 11:08
  • possible duplicate of [Is it possible to run two AsyncTask in same time?](http://stackoverflow.com/questions/18357641/is-it-possible-to-run-two-asynctask-in-same-time) – Amsheer Oct 03 '14 at 14:01

3 Answers3

1

You have to use multiple thread to run the different different services on one activity.

Ravind Maurya
  • 977
  • 15
  • 24
  • Yes, it is another option to handle it. But I want to know that can it be possible with asyncTask? – unflagged.destination May 22 '14 at 10:35
  • list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view,int position, long id) {fileDownloader.executeOnExecutor( AsyncTask.THREAD_POOL_EXECUTOR, MidnightUtility.KEY_DOWNLOAD_PATH); } – unflagged.destination May 22 '14 at 10:43
  • @unflagged.destination according to your scenario which is mention above you have to use multiple thread for multiple download load service. – Ravind Maurya May 22 '14 at 10:46
0

Create object inside the loop.

void stratTask(){
    for(String url:urls){
    AsyncTask asyncTask=new AsyncTask();
     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){

            asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
      }
      else{
            asyncTask.execute(url);
        }
    }
}
Sathish
  • 33
  • 4
0

It looks like your problem is in how you define fileDownloader:

 // Click event for single list row 
list.setOnItemClickListener(new OnItemClickListener() {
 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 Toast.makeText(activity, "List Item Clicked", Toast.LENGTH_LONG) .show(); 
fileDownloader.executeOnExecutor( AsyncTask.THREAD_POOL_EXECUTOR, Utility.KEY_DOWNLOAD_PATH); 

You appear to be defining the fileDownoader outside of the onclick event. So you actually only have one AsyncTask, and when execute fileDownloader.executeOnExecuter you are overwriting and previous AsyncTask assigned to that object.

You may want to change your FileDownloader class so that it contains some list of single AsyncTasks ( List ) instead, although that is not the only way to implement what you are looking for.

paul0-ts
  • 61
  • 2