我有一个webview,基本上能够拦截各种链接,视频,apks,hrefs.
现在,我想要的是,一旦我从网址下载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");
比应用程序崩溃…
关于该怎么做的任何想法?
编辑:我能够自动启动下载和安装包(使用sleep()):
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;
并且,维生素建议:
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);
}
但是,我无法捕获下载完成的确切时间,可能需要创建我自己的下载功能而不使用浏览器的一个,任何想法?