如何打开webview上的每个链接到chrome自定义选项卡?

时间:2016-11-29 10:57:33

标签: java android webview chrome-custom-tabs

我想在点击或标签链接时打开webview上的每个链接到Chrome自定义标签。

final WebView webView = (WebView) findViewById(R.id.web_view);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            webView.loadUrl(uri);
            return true;
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            if (url.startsWith(uri)){
                webView.loadUrl(uri);
            }else{
                Uri uri = Uri.parse(url);
                CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
                builder.build().launchUrl(MainActivity.this, uri);
            }
        }
    });

1 个答案:

答案 0 :(得分:1)

您必须在外部网络浏览器中重定向您的网址,例如

final WebView webView = (WebView) findViewById(R.id.web_view);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("http")) { // Could be cleverer and use a regex
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                mContext.startActivity(intent);
                return true;
            }
            return false;
        }

        @Override
        public void onLoadResource(WebView view, String url) {
        }
    });