How to make links open within webview or open by default browser depending on domain name?

后端 未结 3 1561
梦毁少年i
梦毁少年i 2020-12-16 05:56

I have WebView in which I want to open links belong to domain www.example.org in webview while all other links (if clicked) open by the default browser outside of my applica

3条回答
  •  情歌与酒
    2020-12-16 06:52

    Add the following to your activity

    @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    if(Uri.parse(url).getHost().endsWith("192.168.1.34")) {
                        view.loadUrl(url);
                        Log.d("URL => ", url);    // load URL in webview
                        return false;
                    }
    
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    view.getContext().startActivity(intent); // Pass it to the system, doesn't match your domain 
                    return true;
                }
    

提交回复
热议问题