title: Use DownloadManager class to download file from WebView
body: <p>I have this little code here when click a link on webview that is a link to a file, in this case .mp4. this code will go to default web browser and request app that can view this file type.</p>
  
  <pre><code>myWebView.setDownloadListener(new DownloadListener() {
      public void onDownloadStart(String url, String userAgent,
          String contentDisposition, String mimetype,
          long contentLength) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
      }
  });
  </code></pre>
  
  <p>What i want is when i click that file link, it create a dialog that ask weather to download or view the file. If click download, i want to use DownloadManager class to handle it and download that file in the background and alert when completed. And if click view, i want to create intent that ask for app that can view this file without going to the web browser. </p>
  
  <pre><code>    private class MyWebViewClient extends WebViewClient {
  
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, final String url) {
  
          if (url.endsWith(".mp4")) {
              AlertDialog.Builder builder = new AlertDialog.Builder(context);
              builder.setMessage(R.string.dialog_title)
                  .setCancelable(false)
                  .setPositiveButton(R.string.dialog_download, new DialogInterface.OnClickListener() {                        
  
                      public void onClick(DialogInterface dialog, int id) {
                                  DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                                  request.setDescription("Some descrition");
                                  request.setTitle("Some title");
                                  // in order for this if to run, you must use the android 3.2 to compile your app
                                  request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext");
  
                                  // get download service and enqueue file
                                  DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                                  manager.enqueue(request);
                      }
                  })
                  .setNegativeButton(R.string.dialog_play, new DialogInterface.OnClickListener() {
  
                      public void onClick(DialogInterface dialog, int id) {
                          WebView myWebView = (WebView) findViewById(R.id.webview);                           
                          myWebView.setDownloadListener(new DownloadListener() {
                              public void onDownloadStart(String url, String userAgent,
                                      String contentDisposition, String mimetype,
                                      long contentLength) {
                                  Intent intent = new Intent();
                                  intent.setAction(Intent.ACTION_VIEW);
                                  intent.setDataAndType(Uri.parse(url), "video/mp4");
                                  startActivity(intent);
                              }
                          });
  
                      }
                  });
              AlertDialog alert = builder.create();
              alert.show();
          }
          return false;
      }
  }
  </code></pre>
  
  <p>Now i got this code that prompt user to download or play mp4 file that the user clicked. But when i click Play or Download it is work until i click the link the second time, what's wrong with the code above if anyone can correct this please. thanks.</p>
  
  <p>I'm very new to Android developing and java, if anyone could guide me through this it will help me learn faster. And also sorry for my English...</p>