문제

Ok, there will be two types of links in the content of my WebView. The behavior will be defined by the format of the link.

(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")

I am not sure if it is possible to create a map for the WebView which maps from a url pattern to an intent. For example, by default, if the url begins with "mailto:" the WebView will create an intent to open the mailbox. Could I define other mappings for my WebView?

I know there is a way to set a WebViewClient and override the shouldOverrideUrlLoading() method. But in API level 19, the function is not guaranteed to be called:

shouldOverrideUrlLoading() not called

So is it possible to set this url pattern to intent mapping as a general settings of the WebView?

도움이 되었습니까?

해결책

Using a WebViewClient should be enough. We've had no problems with API level 19. For example:

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;
    }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top