Support for other protocols in Android webview

前端 未结 7 2183
囚心锁ツ
囚心锁ツ 2020-11-29 04:08

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 s

7条回答
  •  广开言路
    2020-11-29 04:46

    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.

提交回复
热议问题