H5与原生交互-jsbridge

2018-06-18  本文已影响0人  码农朱同学

在h5端:

<p style="text-align:center;">
    <input type="button" id="intercept" value="拦截" onclick="backIntercept();"
           style="font-size:50px;width:500px;height:75px;text-align:center;"/>
</p>
<script>
        function testH5Dialog(){
            if(confirm("请按'放弃赚钱'或'继续支付'进行选择")){
                alert("您按下了'放弃赚钱'按钮");
            } else {
                alert("您按下了'继续支付'按钮");
            }
        }

        function backIntercept() {
            window.WebViewJavascriptBridge.callHandler(
                'backIntercept'
                , {'force': '1', 'action': 'dialog'}
                , function(responseData) {

                }
            );
        }


        function connectWebViewJavascriptBridge(callback) {
            if (window.WebViewJavascriptBridge) {
                callback(WebViewJavascriptBridge)
            } else {
                document.addEventListener(
                    'WebViewJavascriptBridgeReady'
                    , function() {
                        callback(WebViewJavascriptBridge)
                    },
                    false
                );
            }
        }

        connectWebViewJavascriptBridge(function(bridge) {
            bridge.init(function(message, responseCallback) {
                console.log('JS got a message', message);
                var data = {
                    'Javascript Responds': '测试中文!'
                };
                console.log('JS responding with', data);
                responseCallback(data);
            });

            bridge.registerHandler("fromNative", function(data, responseCallback) {
                document.getElementById("show1").innerHTML = ("FromNative = " + data);
                var responseData = "你好fromNative";
                responseCallback(responseData);
            });

            bridge.registerHandler("dialog", function(data, responseCallback) {
                testH5Dialog()
                responseCallback(responseData);
            });
        })

</script>

我们在代码中加上

@OpenNavigation(key = "backIntercept")
public class BackInterceptOpenNavigationHandler extends BaseWebViewOpenNavigationHandler {

    @Override
    public void navigation(Context context, String paramsJsonData, IOpenNavigationCallBack callBack) {
        try {
            JSONObject jsonObject = new JSONObject(paramsJsonData);
            int force = jsonObject.optInt("force");
            String action = jsonObject.optString("action");
            //1一直拦截,0只拦一次,-1一直不拦
            if (force == -1) {
                resumeClick(context);
                return;
            }
            if (getJsUpdataWebViewListener() instanceof WebViewLayout) {
                WebViewLayout webviewLayout = (WebViewLayout) getJsUpdataWebViewListener();
                webviewLayout.getTitleBar().getBackIV().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        webviewLayout.getWebView().callHandler(action, paramsJsonData, null);
                        if (force == 0) {
                            resumeClick(context);
                        }
                    }
                });
                webviewLayout.getTitleBar().getLeftTV().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        webviewLayout.getWebView().callHandler(action, paramsJsonData, null);
                        if (force == 0) {
                            resumeClick(context);
                        }
                    }
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void resumeClick(Context context) {
        if (getJsUpdataWebViewListener() instanceof WebViewLayout) {
            WebViewLayout webviewLayout = (WebViewLayout) getJsUpdataWebViewListener();
            webviewLayout.getTitleBar().getBackIV().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (webviewLayout.getWebView().canGoBack() && webviewLayout.getWebViewParams().mIsBack) {
                        webviewLayout.getWebView().goBack();
                    } else {
                        if (context instanceof Activity) {
                            ((Activity) context).finish();
                        }
                    }
                }
            });
            webviewLayout.getTitleBar().getLeftTV().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (context instanceof Activity) {
                        ((Activity) context).finish();
                    }
                }
            });
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读