2014-04-29 157 views
1

好吧,我的WebView的内容中将有两种类型的链接。行为将由链接的格式定义。创建意图点击WebView链接

(1) Open the link in a browser. (The url begins with "openbrowser:") 
(2) According to the link, open another Activity in the same project. 
(The url will be "openactivity") 

我不确定是否有可能为WebView创建映射从url模式到意图的映射。例如,默认情况下,如果网址以“mailto:”开头,则WebView将创建打开邮箱的意图。我可以为我的WebView定义其他映射吗?

我知道有一种方法来设置WebViewClient并覆盖shouldOverrideUrlLoading()方法。但是,在API级别19,功能不能保证被称为:

shouldOverrideUrlLoading() not called

所以是有可能这个网址模式的意图映射设置为WebView中的一般设置?

+0

我正在使用Kit Kat设备使用此方法(自定义模式),并且没有遇到任何问题。你测试过了吗? – matiash

+0

在我的情况下,要在WebView中显示的数据作为HTML字符串从服务器返回。因此,我将使用contentWebView.loadData(htmlString,“text/html”,“utf-8”)加载数据。我不确定这是否是你的情况。 – darklord

+0

这就是我们的情况。请参阅答案中的示例。 :) – matiash

回答

1

使用WebViewClient应该足够了。我们对API级别19没有任何问题。例如:

WebView webView = new WebView(this); 
String html = "<html><body><a href=\"showmessage:hello%20there\">Test it</a></body></html>"; 
webView.loadData(html, "text/html", "utf-8"); 
webView.setWebViewClient(new WebViewClient() 
{ 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    { 
     if (url.startsWith("showmessage")) 
     { 
      Toast.makeText(MainActivity.this, url, Toast.LENGTH_SHORT).show(); 
      return true; 
     } 
     return false; 
    } 
});