2

I want to know mechanism of Download Managers (not the DownloadManager class introduced in API 9), as in my current project, I need to implement a few things same as download manager.

My basic understanding is that it uses a Service to download files. But what I cannot understand is how will it handle the multiple file download request, e.g. A file is currently being downloaded. Now user adds another file to be downloaded. I don't think a single Service can handle it. Can it? or is there some other method to do it? Any suggestions?

PS forgive me if the question is not clear enough because I myself don't understand my doubt very clearly. :(

ashu
  • 1,756
  • 4
  • 22
  • 41

1 Answers1

1

They are using a MultiThreaded Socket Wheel.

Means, there is a ForegroundService which handle different Threads within the Service.

The ForegroundService make sure that it will be kept alive. The Threads itself are single Processes which run in a Background Thread.

Threre are several ThreadExecutors available which make sure that you have only a few Threads running paralell or process them thread by thread.

Here is a good tutorial making a SocketWheel http://www.javaspecialists.eu/archive/Issue023.html

Sources:

ThreadExecutor http://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor.html

ThreadPoolExecutor ExectuorService vs ThreadPoolExecutor (which is using LinkedBlockingQueue)

ForegroundService Android - implementing startForeground for a service?

Edited: for some code

public class DLService extends Service {

@Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  Notification note=new Notification(R.drawable.somegraphics, "some title", randomnr);
  Intent i=new Intent(this, ActivityClassToOpenWhenClicked.class);
  i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
  PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0);
  note.setLatestEventInfo(this, "downloadmanager","downloading.. nothing", pi);
  note.flags|=Notification.FLAG_NO_CLEAR;
  startForeground(1337, note); 
 // if (intent.hasExtra("dlurl")) { 
    new Thread(new Runnable() {
         public void run() {
        new Client("http://yourfile.com/file.jpg");     
     }
    }).start();
  return START_NOT_STICKY;
 }

class Client {
  public Client(String filetodownload) throws Exception {
  //do your http connection. for example one download  #
  // HttpUrlConnection httpConnection = new .....
 }
}
}

Its just a quick coded example, not tested. But it shows the concept. The service itself accept a Intent which can be a url where the file is located. Then it creates a new Thread which do the job. The Thread will run aslong as it takes to download the file. The foregroundService makes sure that the Service will be kept alive. You can also create a ThreadPoolExecutor which handle multiple Thread at onec. Read http://developer.android.com/training/multiple-threads/create-threadpool.html for instructions.

Community
  • 1
  • 1
Emanuel
  • 8,027
  • 2
  • 37
  • 56
  • I think you didnt read my answer carefully :-) And yes, a single service can handle this. A Service is created and the Service itself creates several Threads which creates a Socket (or HTTPConnection). The Thread itself can communicate back to the Service by sending Intent Extras or using Listeners. – Emanuel Jun 09 '14 at 14:07
  • sorry! i didn't read your edited answer and made a comment base on previous source that didn't make it clear to me. So basically i need to implement ThreadPoolExecutor in service to do my work? – ashu Jun 09 '14 at 14:14
  • Ill post some psuedo Code, wait... :-) – Emanuel Jun 09 '14 at 14:19
  • That will of so much help! :) – ashu Jun 09 '14 at 14:24
  • updated. Its just pseudocode. Dont except it to work but it will show you how to do that. – Emanuel Jun 09 '14 at 14:28
  • Well! thanks. Now i understand it. Probably my first time experience with ThreadPoolExecutor was the only problem. :) – ashu Jun 09 '14 at 14:44