JsBridge为BridgeWebView添加自定义跳转协议

JsBridge: https://github.com/lzyzsd/JsBridge 自带的BridgeWebView并未添加yy://之外的协议,需要自行实现,如通用协议如电话、短信、邮件以及自定义协议如打开微信等。

<a href="tel:10086">电话</a>
<a href="sms:10086">短信</a>
<a href="mailto:yezhou@yezhou.org">邮件</a>
<a href="weixin://">打开微信</a>

private WebViewClient getWebViewClient() {
    return new WebViewClient() {
        BridgeWebViewClient mBridgeWebViewClient = new BridgeWebViewClient(mBridgeWebView);

        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            NLog.i(Constants.TAG, "url: " + request.getUrl().toString());
            String url = request.getUrl().toString();
            if (shouldOverrideUrlLoading(url)) {
                return true;
            } else {
                return mBridgeWebViewClient.shouldOverrideUrlLoading(view, request);
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            NLog.i(Constants.TAG, "url: " + url);
            if (shouldOverrideUrlLoading(url)) {
                return true;
            } else {
                return mBridgeWebViewClient.shouldOverrideUrlLoading(view, url);
            }
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            mBridgeWebViewClient.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            //super.onPageFinished(view, url);
            mBridgeWebViewClient.onPageFinished(view, url);
        }

        @TargetApi(Build.VERSION_CODES.M)
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            //yy://__QUEUE_MESSAGE__/
            LogUtils.i(TAG, "onReceivedError:" + error.getDescription() + " code:" + error.getErrorCode() + ", url: " + request.getUrl().toString());
        }

        private boolean shouldOverrideUrlLoading(String url) {
            if (url.startsWith(WebView.SCHEME_TEL) || url.startsWith(SCHEME_SMS) || url.startsWith(WebView.SCHEME_MAILTO)) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                LianLianServiceActivity.this.startActivity(intent);
                return true;
            } else if (url.startsWith("weixin:")) {
                //跳转微信
                Intent launch = getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
                if (launch == null) {
                    Toast.makeText(LianLianServiceActivity.this, "Wechat not installed", Toast.LENGTH_SHORT).show();
                    return true;
                }
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(launch.getComponent());
                startActivity(intent);
                return true;
            }
            return false;
        }
    };
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/18/jsbridge-add-custom-jump-protocol-for-bridgewebview/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
JsBridge为BridgeWebView添加自定义跳转协议
JsBridge: https://github.com/lzyzsd/JsBridge 自带的BridgeWebView并未添加yy://之外的协议,需要自行实现,如通用协议如电话、短信、邮件以及自定义协议如……
<<上一篇
下一篇>>
文章目录
关闭
目 录