title: Using DownloadManager to download unknown number of files
body: <p>I hope this question is ok. I'm having some issues wrapping my head around how to solve this issue: I want to use DownloadManager to download XML files from a web server. The XML files have an info section that contains, among other things, the total number of entries in the database. Each XML file has up to 1000 entries, and the total number of entries can be several thousand, so I need to kick off new download sessions dynamically until everything is downloaded. It's important that the app continues to download, parse the data and save it to the apps SQLite database, even if the app is not active. </p>
  
  <p>I have tried to solve this by:</p>
  
  <ol>
  <li><p>Creating a DownloadIntentService class, extending IntentService</p></li>
  <li><p>Having my MainActivity class implementing DownloadResultReceiver.Receiver where DownloadResultReceiver looks like this:</p>
  
  <pre><code>public class DownloadResultReceiver extends ResultReceiver {
      private Receiver mReceiver;
  
      public DownloadResultReceiver(Handler handler) {
          super(handler);
      }
  
      public void setReceiver(Receiver receiver) {
          mReceiver = receiver;
      }
  
      public interface Receiver {
          public void onReceiveResult(int resultCode, Bundle resultData);
      }
  
      @Override
      protected void onReceiveResult(int resultCode, Bundle resultData) {
          if (mReceiver != null) {
              mReceiver.onReceiveResult(resultCode, resultData);
          }
      }
  }
  </code></pre></li>
  <li><p>In my MainActivity class, if my database is not populated, I call initServiceDownload():</p>
  
  <pre><code>private void initServiceDownload(String sessionId, long offset)
  {
      /* Starting Download Service */
      mReceiver = new DownloadResultReceiver(new Handler());
      mReceiver.setReceiver(this);
  
      Intent intent = new Intent(DownloadManager.ACTION_DOWNLOAD_COMPLETE, null, this, DownloadIntentService.class);
  
      /* Send optional extras to Download IntentService */
      intent.putExtra("url", Client.getUrl(sessionId, offset));
      intent.putExtra("receiver", mReceiver);
      intent.putExtra("database", userDatabase);
      intent.putExtra("session", sessionId);
      intent.putExtra("offset", offset+"");
  
      startService(intent);
  }
  </code></pre></li>
  <li><p>In my DownloadIntentService's onHandleIntent I try to download file using DownloadManager</p></li>
  <li><p>In my MainActivity, I override onReceiveResult()from DownloadResultReceiver, and if result is DownloadIntentService.STATUS_FINISHED I want to parse the files, then kick off another DownloadManager session, until done. Only problem is I never get into this method. </p></li>
  </ol>
  
  <p>Am I on the right track here? And further, should I implement a second section in the IntentService class to parse the data, then kick off another session from there?</p>
  
  <p>I have been googling around a lot trying to find information on the optimal way to do it, so a few pointers would be great! :-)</p>