安卓 - 在网页视图中点击链接用户



如何获取用户在 Web 视图中单击的链接? 示例:我的网络视图将用户带到一个列出信息和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/");
}

我认为您要覆盖的方法是WebViewClient.shouldInterceptRequest(WebView view,String url)

public WebResourceResponse shouldInterceptRequest (WebView view, String url)
  • 将资源请求通知主机应用程序,并允许应用程序以返回数据。如果返回值为 null,则WebView 将继续像往常一样加载资源。否则,将使用返回响应和数据。注意:此方法由网络线程,因此客户端在访问时应谨慎行事私人数据。参数

请参阅网页视图应该拦截请求示例

最新更新