title: Downloading file with DownloadManager Android
body: <p>I am trying to download a file using the DownloadManager inside an AsyncTask</p>
  
  <pre><code>private class DownloadTask extends AsyncTask&lt;String, Void, Boolean&gt;
  {
  
      private Context mContext;
  
      public DownloadTask(Context context)
      {
          mContext = context;
      }
  
      @Override
      protected Boolean doInBackground(String... strings) {
  
  
          String fileName = strings[2]+"_"+strings[3]+ strings[4];
          String destination = mDestination + fileName;
          final Uri uri = Uri.parse("file://" + destination);
          mDownloading = true;
          //If the file is already downloading just return.
          File file = new File(destination);
          if (file.exists()) {
              return true;
          }
  
  
  
          //set downloadmanager
          DownloadManager.Request request = new    DownloadManager.Request(Uri.parse(strings[0]));        
          request.setDescription(mContext.getString(R.string.downloading)+ " "+strings[1]);
          request.setTitle(mContext.getString(R.string.downloading_title));
  
          //set destination
          request.setDestinationUri(uri);
  
          // get download service and enqueue file
          final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
  
          final long downloadId = manager.enqueue(request);
  
  
          //set BroadcastReceiver to enable next download
          BroadcastReceiver onComplete = new BroadcastReceiver() {
              public void onReceive(Context ctxt, Intent intent) {
                  unregisterReceiver(this);
                  mDownloading = false;
              }
          };
          //register receiver for when file download is compete
          registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
          return true;
      }
  }`
  </code></pre>
  
  <p>The code works fine when I pass the url of the file. The thing is, that we want to make a GET call to a WEB made on PHP. This WEB method creates or selects a file and redirects using  header<code>("Location: ".$database-&gt;single()['Url']);</code></p>
  
  <p>But when we make the call from DownloadManager it just calls registerReceiver right away.</p>
  
  <p>Does anyone knows why this happens?</p>
  
  <p>If we use a <code>HttpURLConnection</code> it works fine, but we would like to delegate all the hard work of the download in the DownloadManager.</p>
  
  <p>Thank you for your comments.</p>