我有一个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);
}
但是,我无法捕获下载完成的确切时间,可能需要创建我自己的下载功能而不使用浏览器的任何想法?
答案 0 :(得分: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);
}
答案 1 :(得分: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);
答案 2 :(得分:1)
由于Android安全模型,无法自动安装Apk文件。
答案 3 :(得分: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));