Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download multiple files using a Service in android

My application has a lot of optional data that can be downloaded so I decided to use a Service to handle all the downloads in the background, So I started learning it and here is where i got:

public class DownloadService extends IntentService{

    public DownloadService() {
        super("DownloadService");

    }

    @Override
    protected void onHandleIntent(Intent intent) {

        String URL=intent.getStringExtra("DownloadService_URL");
        String FileName=intent.getStringExtra("DownloadService_FILENAME");
        String Path=intent.getStringExtra("DownloadService_PATH");

        try{
        URL url = new URL(URL);
        URLConnection conexion = url.openConnection();

        conexion.connect();


        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(Path+FileName);

        byte data[] = new byte[1024];

        int count = 0;
        while ((count = input.read(data)) != -1) {
            output.write(data);
        }

        output.flush();
        output.close();
        input.close();

        }
        catch(Exception e){ }
    }

}

The code from the main activity:

        Intent ServiceIntent = new Intent(this,DownloadService.class);
        ServiceIntent.putExtra("DownloadService_URL", "the url...");
        ServiceIntent.putExtra("DownloadService_FILENAME", "Test1.rar");
        ServiceIntent.putExtra("DownloadService_PATH", "/sdcard/test/");
        startService(ServiceIntent);
  1. Is the code used to download the files correct? Am I using the Service correctly?
  2. I want to download a lot of files.. So should I startService for each different URL?
  3. I would like to inform the user of the percentage done.. But the Service doesnt have a UI. Should I do that in the notification bar?

Thanks.

like image 387
Omar Avatar asked Aug 27 '11 15:08

Omar


People also ask

How do I download multiple files at once on Android?

Create a table to add the items which you want to download. Sorting should be based on the time when item was added for download. Consider this as a queue. Also add one column to check if a file is already downloaed or not.


1 Answers

Is the code used to download the files correct?

I don't like the use of concatenation to create fully-qualified file paths (use the appropriate File constructor). Catching exceptions and not doing anything with them is a really bad idea. On Android 2.3 and higher, you should consider using DownloadManager.

Otherwise, it's probably OK for basic stuff.

I want to download a lot of files.. So should I startService for each different URL?

That should work fine. Note that they will be downloaded one at a time, as IntentService has only one background thread.

I would like to inform the user of the percentage done.. But the Service doesnt have a UI. Should I do that in the notification bar?

That would be one solution. A variation on that would be to have the service send an ordered broadcast, to be picked up by your activity if it is still on-screen or by a BroadcastReceiver that would do the Notification. Here is a blog post with more on that, and here is a tiny sample application demonstrating the concept.

like image 152
CommonsWare Avatar answered Oct 23 '22 11:10

CommonsWare