public boolean startDownloadAndWait(String targetUrl,long timeout){
  if (targetUrl.length() == 0 || targetUrl == null) {
    Log.v(LOG_TAG,"Empty or Null target url requested to DownloadManager");
    return true;
  }
  Request request=new Request(Uri.parse(targetUrl));
  long enqueue=mDownloadManager.enqueue(request);
  Log.v(LOG_TAG,"Sending download request of " + targetUrl + " to DownloadManager");
  long startTime=System.currentTimeMillis();
  while (true) {
    if ((System.currentTimeMillis() - startTime) > timeout) {
      Log.v(LOG_TAG,"startDownloadAndWait timed out, failed to fetch " + targetUrl + " within "+ timeout);
      return downloadSuccessful(enqueue);
    }
    Log.v(LOG_TAG,"Waiting for the download to finish " + targetUrl);
synchronized (mDownloadMonitor) {
      try {
        mDownloadMonitor.wait(SHORT_TIMEOUT);
      }
 catch (      InterruptedException e) {
        e.printStackTrace();
      }
      if (!downloadSuccessful(enqueue)) {
        continue;
      }
      return true;
    }
  }
}

public boolean startDownloadAndWait ( String targetUrl , long timeout ) { if ( targetUrl . length ( ) == 0 || targetUrl == null ) { Log . v ( LOG_TAG , "Empty or Null target url requested to DownloadManager" ) ; return true ; } Request request = new Request ( Uri . parse ( targetUrl ) ) ; long enqueue = mDownloadManager . enqueue ( request ) ; Log . v ( LOG_TAG , "Sending download request of " + targetUrl + " to DownloadManager" ) ; long startTime = System . currentTimeMillis ( ) ; while ( true ) { if ( ( System . currentTimeMillis ( ) - startTime ) > timeout ) { Log . v ( LOG_TAG , "startDownloadAndWait timed out, failed to fetch " + targetUrl + " within " + timeout ) ; return downloadSuccessful ( enqueue ) ; } Log . v ( LOG_TAG , "Waiting for the download to finish " + targetUrl ) ; synchronized ( mDownloadMonitor ) { try { mDownloadMonitor . wait ( SHORT_TIMEOUT ) ; } catch ( InterruptedException e ) { e . printStackTrace ( ) ; } if ( ! downloadSuccessful ( enqueue ) ) { continue ; } return true ; } } }
start a download on a given url and wait for completion .
