Question

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?

Was it helpful?

Solution

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;
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top