ashu
ashu

Reputation: 1826

How do download managers work on Android?

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. :(

Upvotes: 2

Views: 211

Answers (1)

Emanuel
Emanuel

Reputation: 8106

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.

Upvotes: 1

Related Questions