2014-04-29 85 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; 
    } 
});