1

Scenario:

Activity starts a service to download n files(zip files)[n varies from 10 to 60, size of each file varies from 100 kb to 20 MB] sends List<String> urlsToBeDownloaded

In Service start method, first one file is kept for download, once file is downloaded(Using Android's Download manager) there is a broadcast receiver which receives and process the file for unzipping. Once file is zipped, another file is kept for download. The code is like this:

public class DownloadService extends Service {

List<String> urlsToBeDownloaded;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
   // set urlsToBeDownloaded
   registerReceiver(onComplete, new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE));
   Utility.downloadFromUrl(urlsToBeDownloaded.get(0),this);
}

BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        new ProcessNext().execute(intent);
    }
};

  ProcessNext extends AsyncTask<Intent, Integer, Integer> {
    @Override
    protected Integer doInBackground(Intent... params) {
        return processNext(params[0]);
    }
 }

 protected int processNext(Intent intent) {
    Query query = new Query();
    ObjectOutputStream oos = null;
        Long dwnId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,
                0);
        query.setFilterById(dwnId);
        DownloadManager manager = (DownloadManager) (this)
                .getSystemService(Context.DOWNLOAD_SERVICE);
        Cursor cursor = manager.query(query);
        if (cursor.moveToFirst()) {
            int status = cursor.getInt(cursor
                    .getColumnIndex(DownloadManager.COLUMN_STATUS));
            String title = cursor.getString(cursor
                    .getColumnIndex(DownloadManager.COLUMN_TITLE));
            File f1 = new File(Environment
                    .getExternalStoragePublicDirectory(
                            Environment.DIRECTORY_DOWNLOADS)
                    .getAbsolutePath()
                    + "/" + title);
            if (status == DownloadManager.STATUS_SUCCESSFUL) {
                 Utility.extractFile(title);
                 urlsToBeDownloaded.remove(0);
                 if(urlsToBeDownloaded.size() != 0)
                     Utility.downloadFromUrl(urlsToBeDownloaded.get(0))
                 else
                     //show done message
            }
           }
          }
     }

What I'm trying to do here is in sequence, means a file will be processed for download only if the previous file is downloaded and extracted.

Problem:

When I see download folder, there are multiple files getting downloaded at a time. I am not able to understand how multiple files are downloading. Is there a way to fix it to sequential download?

Community
  • 1
  • 1
Jainendra
  • 24,713
  • 30
  • 122
  • 169
  • Why is it a problem that multiple files are downloaded at the same time? Why don't you use a producer/consumer model? – fge Nov 05 '14 at 07:11
  • did you try to use intent service – N Sharma Nov 05 '14 at 07:41
  • @Williams I tried but using IntentService doesn't hit broadcast receiver. See [this](http://stackoverflow.com/questions/26502993/registerreceiver-from-intentservice-doesnt-hit-broadcastreceiver) – Jainendra Nov 05 '14 at 09:11

0 Answers0