2012-05-09 56 views
0

如何获取用户在webview中点击的链接?示例:如果用户点击PDF,我的webview会将用户带到列出信息和PDF的网站。我想要获取用户点击的PDF文件的链接并将其放入Google的在线查看器。我有的代码不起作用,因为它不拦截点击链接。有任何想法吗?Android - 在webview中获取链接用户点击

以下是我有:

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/"); 
} 

回答

4

我想你想覆盖的方法是WebViewClient.shouldInterceptRequest(WebView view, String url)

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

看到webview shouldinterceptrequest example

+0

我看到最小的SDK版本是11,因为在这个例子说。我正在为版本10构建(对于仍然在2.3.3姜饼上的人)。我唯一的选择是升级到下一个版本,11? – user1363871

相关问题