2011-05-24 166 views
2

我有一个基本上能够拦截各种链接,视频,apks,hrefs的web视图。Android的自动安装APK

现在,我要的是有一次我从一个网址下载一个APK,它会自动安装:

这是shouldOverrideUrlLoading()代码的一部分:

 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; 

如果我添加

intent.setDataAndType(Uri.parse(url), "application/vnd.android.package-archive"); 

比应用程序崩溃...

任何想法,为w做帽子?

编辑:我是能够自动启动程序包的下载和安装(使用睡眠()):

 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; 

,并为vitamoe建议:

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); 
} 

然而,我无法捕获完成下载的确切时间,可能需要创建我自己的下载功能,而不是使用浏览器的一个,任何想法?

+1

如果您发布了错误日志,它会有帮助。 – DKIT 2011-05-24 10:56:12

+0

请发布整个webview.java文件 – 2011-09-08 23:37:34

回答

3

要下载没有浏览器的文件做某事像这样:

String apkurl = "http://your.url.apk"; 
InputStream is; 
try { 
    URL url = new URL(apkurl); 
    HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
    con.setRequestMethod("GET"); 
    con.setDoOutput(true); 
    con.connect(); 
    is = con.getInputStream(); 
} catch (SSLException e) { 
    // HTTPS can end in SSLException "Not trusted server certificate" 
} 

// Path and File where to download the APK 
String path = Environment.getExternalStorageDirectory() + "/download/"; 
String fileName = apkurl.substring(apkurl.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(); 

// finally, install the downloaded file 
install(path + fileName); 
1

由于Android安全模型,无法自动安装Apk文件。

+0

是的。您可以触发流程的开始,但如果没有用户同意,它将无法完成。 – 2011-05-24 14:57:08

+0

我不希望它完成,我想安装提示。 – 2011-05-25 06:26:13

3

你可以温度。将其下载到SD卡,使用软件包管理器进行安装,然后再将其删除。

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); 
} 
+0

那么,我正在使用浏览器来下载apk,现在当我使用你给的片段时,它开始运行包管理器,但是说“无法解析包” – 2011-05-24 13:08:51

+0

你确定,包是好的吗?当你在你最喜欢的文件管理器中点击它时,它会安装吗? – 2011-05-31 10:56:07

+0

此安装方法正常工作,但此解决方案的问题在于设备不知道文件何时完成下载。所以我所做的就是打开一个asyn DoInBackground()函数来下载文件,并使用您在下载完成时提供的这种方法,而且一切正常。 – 2011-05-31 12:28:59

1

为什么不尝试使用下载管理和广播接收器,当下载完成时会截获?下载管理器适用于Android 2.3以上版本虽然

这里举例:

myWebView.setWebViewClient(new WebViewClient() { 
    @Override 
    public void onReceivedError(WebView view, int errorCode, 
     String description, String failingUrl) { 
      Log.d("WEB_VIEW_TEST", "error code:" + errorCode + " - " + description); 
    } 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      // handle different requests for different type of files 
      // this example handles downloads requests for .apk and .mp3 files 
      // everything else the webview can handle normally 
      if (url.endsWith(".apk")) { 
       Uri source = Uri.parse(url); 
       // Make a new request pointing to the .apk url 
       DownloadManager.Request request = new DownloadManager.Request(source); 
       // appears the same in Notification bar while downloading 
       request.setDescription("Description for the DownloadManager Bar"); 
       request.setTitle("YourApp.apk"); 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
        request.allowScanningByMediaScanner(); 
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
       } 
       // save the file in the "Downloads" folder of SDCARD 
       request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "SmartPigs.apk"); 
       // get download service and enqueue file 
       DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
       manager.enqueue(request); 
      } 
      else if(url.endsWith(".mp3")) { 
       // if the link points to an .mp3 resource do something else 
      } 
      // if there is a link to anything else than .apk or .mp3 load the URL in the webview 
      else view.loadUrl(url); 
      return true;     
    } 
}); 
这里

完整的答案:用户bboydflo Downloading a file to Android WebView (without the download event or HTTPClient in the code)

广播接收机截获时,下载完成后

private BroadcastReceiver onDownloadComplete = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { 
      // toast here - download complete 
     } 
    } 
}; 

记在如下主要活动中加入收受者:

registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));