title: Android - DownloadListener or AsyncTask
body: <p>am trying to download from web page some files by clinking the url with webview handling the download not the browser </p>
  
  <p>if i use DownloadListener  it works perfectly with one problem i cant see the progressbar</p>
  
  <p>if i use the AsyncTask i have to put the url in the code to download it i can just click the url and start downloading</p>
  
  <p>my question is how can i let the AsyncTask download any url from the web without sitting the</p>
  
  <p>downloadFile.execute("the url to the file you want to download");</p>
  
  <p>or how i can  create progressbar for DownloadListener  </p>
  
  <p><i></p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      webview = (WebView) findViewById(R.id.webview);
      myProgressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
      new Thread(myThread).start();
      webview.setWebViewClient(new HelloWebViewClient());
      webview.getSettings().setJavaScriptEnabled(true);
      webview.setInitialScale(50); 
      webview.getSettings().setUseWideViewPort(true); 
      webview.setVerticalScrollBarEnabled(false);
      webview.setHorizontalScrollBarEnabled(false);
      webview.loadUrl("http://localhost/index.php");
      webview.setWebViewClient(new DownloadWebViewClient());
      webview.setDownloadListener(new DownloadListener() {
  
         @Override
         public void onDownloadStart(String url, String userAgent,
               String contentDisposition, String mimetype,
               long contentLength) {
             InputStream is;
             try {
                 URL u = new URL(url);
                 HttpURLConnection con = (HttpURLConnection) u.openConnection();
                 con.setRequestMethod("GET");
                 con.setDoOutput(true);
                 con.connect();
                 is = con.getInputStream();
  
  
             // Path and File where to download the APK
             String path = Environment.getExternalStorageDirectory() + "/apdroid/";
             String fileName = url.substring(url.lastIndexOf('/') + 1);
             File dir = new File(path);
             dir.mkdirs(); // creates the download directory if not exist
             File outputFile = new File(dir, fileName);
             FileOutputStream fos = new FileOutputStream(outputFile);
  
             // Save file from URL to download directory on external storage
             byte[] buffer = new byte[1024];
             int len = 0;
             while ((len = is.read(buffer)) != -1) {
                 fos.write(buffer, 0, len);
             }
             fos.close();
             is.close();
  
  
             Intent intent = new Intent(Intent.ACTION_VIEW);
             String name = Environment.getExternalStorageDirectory() + "/apdroid/" + url.substring(url.lastIndexOf('/') + 1);
             intent.setDataAndType(Uri.fromFile(new File(name)), "application/vnd.android.package-archive");
             startActivity(intent);
  
  
         }catch (IOException e) {
             e.printStackTrace();
          }
         }
      });
  
  
   }
  
  
  
  protected void install(String fileName) {
      // TODO Auto-generated method stub
  
  }
  
  
  
  private Runnable myThread = new Runnable() {
  
      @Override
      public void run() {
         while (myProgress &lt; 100) {
            try {
               myHandle.sendMessage(myHandle.obtainMessage());
               Thread.sleep(1000);
            } catch (Throwable t) {
            }
         }
      }
  
      Handler myHandle = new Handler() {
  
         @Override
         public void handleMessage(Message msg) {
            myProgress++;
            myProgressBar.setProgress(myProgress);
         }
  
      };
  
   };
  
  
  
  
  
  
  
  
  
  private class HelloWebViewClient extends WebViewClient {
  
  
  
  
  
  
  
  
  
  
  @Override
  public void onReceivedError(WebView view,int errorCode,String description,String failingUrl) {
     try {view.stopLoading();} catch(Exception e){}
     try {view.clearView();} catch(Exception e){}
     view.loadUrl("file:///android_asset/wifi.html");
  }
      }
  </code></pre>
  
  <p></i></p>
  
  <p>i just want to have ProgressBar when i download any file from my page </p>
  
  <p>and i cant use asyncTask because i have to put the files in the code not by clicking at them</p>