Support for other protocols in Android webview

user319940 picture user319940 · Aug 27, 2010 · Viewed 23.6k times · Source

I've created a web view app, the page that is displayed features market:// links but upon clicking them I get the 404 screen along with the error that the protocol is not supported. I've tried looking through documentation but was unable to find anything relating to this. Any help is much appreciated.

Answer

sven picture sven · Apr 1, 2011

For me the JavaScript thing wasn't a solution as the HTML is not under my control. So if you need to control this from the application side, then there is a relative simple solution: Derive from WebViewClientand inject the implementation using WebView.setWebViewClient(). All you need to override in your WebViewClientimplementation is the shouldOverrideUrlLoading method as shown here:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url != null && url.startsWith("market://")) {
        view.getContext().startActivity(
            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        return true;
    } else {
        return false;
    }
}

For me this works fine.