title: Android auto installation of APKs
body: <p>I have a webview which basically is capable of intercepting all sorts of links, video, apks, hrefs.</p>
  
  <p>Now, what I want is once I download an APK from a url, that it'll be auto installed:</p>
  
  <p>This is part of the <code>shouldOverrideUrlLoading()</code> code:</p>
  
  <pre><code>        else if(url.endsWith(".apk")) 
          {
          mWebView.setDownloadListener(new DownloadListener() {
                      public void onDownloadStart(final String url, String userAgent,
                      String contentDisposition, String mimetype,
                      long contentLength) {   
                      }
                      });
          Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(url));
          startActivity(intent);  
          return true;
  </code></pre>
  
  <p>If I add </p>
  
  <pre><code>intent.setDataAndType(Uri.parse(url), "application/vnd.android.package-archive");
  </code></pre>
  
  <p>Than the application crashes...</p>
  
  <p>Any ideas as to what to do?</p>
  
  <p>EDIT: I was able to initiate a download and an installation of the package automatically (using a sleep() ):</p>
  
  <pre><code>        else if(url.endsWith(".apk")) 
          {
          mWebView.setDownloadListener(new DownloadListener() {
                      public void onDownloadStart(final String url, String userAgent,
                      String contentDisposition, String mimetype,
                      long contentLength) {   
                      }
                      });
          Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(url));
          startActivity(intent); 
          String fileName = Environment.getExternalStorageDirectory() + "/download/" + url.substring( url.lastIndexOf('/')+1, url.length() );
          install(fileName);
          return true;
  </code></pre>
  
  <p>and, as vitamoe suggested:</p>
  
  <pre><code>protected void install(String fileName) {
      Intent install = new Intent(Intent.ACTION_VIEW);
      install.setDataAndType(Uri.fromFile(new File(fileName)),
              "application/vnd.android.package-archive");
      startActivity(install);
  }
  </code></pre>
  
  <p>However, I'm unable to capture the exact time that the download is finished, might need to create my own download function and not use the browser's one, any ideas?</p>