我如何获得用户在 webview 中单击的链接?示例:我的 webview 将用户带到列出信息和 PDF 的网站,如果用户单击 PDF,我想获取用户单击的 PDF 文件的链接并将其放入 Google 的在线查看器中。我的代码不起作用,因为它不会拦截点击链接。有任何想法吗?
这是我所拥有的:
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
if (url.startsWith("tel:")) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
} else if (url.startsWith("mailto:")) {
url = url.replaceFirst("mailto:", "");
url = url.trim();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL,
new String[] { url });
startActivity(i);
} else if (url.startsWith("geo:")) {
try {
} catch (Exception e) {
System.out.println(e);
}
} else if (url.endsWith("pdf")) {
try{
String pdf = (url);
webview.loadUrl("http://docs.google.com/viewer?url=" + pdf);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(atcFaa.this, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}
}
else {
view.loadUrl(url);
}
return true;
// then it is not handled by default action
}
});
webview.loadUrl("http://www.somewebsite.com/publications/");
}