【发布时间】:2015-11-26 02:46:53
【问题描述】:
我有网络视图。我将其设置为下载管理器。但是当我用 .apk 打开 url 时,它会自动下载 2 次 apk,并且总是用对话框打开完整的操作。
我的 WebView 代码:
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(MainActivity.this,
"Bağlantı Hatası", Toast.LENGTH_LONG).show();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean shouldOverride = false;
mWebView.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);
}
});
if (url.endsWith(".apk")) {
shouldOverride = true;
Uri source = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(source);
File destinationFile = new File (destinationDir, source.getLastPathSegment());
File to = new File(destinationDir, "BL" + ".apk");
request.setDestinationUri(Uri.fromFile(destinationFile));
manager.enqueue(request);
destinationFile.renameTo(to);
}
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
return true;
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(sbllink);
if (!destinationDir.exists()) {
destinationDir.mkdir();
}
【问题讨论】:
标签: android android-browser android-download-manager