WebViewJavascriptBridge for Android is Cross-platform WebViewJavascriptBridge for Android Extension,the JavaScript interface compatible with WebViewJavascriptBridge 。
- Add following to the build.gradle of your project.
dependencies{compile 'com.gzsll.jsbridge:library:1.1.0' } - Add
com.gzsll.jsbridge.WVJBWebViewto your layout, it is inherited fromWebView.
webView.registerHandler("testJavaCallback", newWVJBWebView.WVJBHandler(){@Overridepublicvoidrequest(Objectdata, WVJBWebView.WVJBResponseCallbackcallback){Toast.makeText(MainActivity.this, "testJavaCallback called:" + data, Toast.LENGTH_LONG).show(); callback.callback("Response from testJavaCallback!")} }); webView.callHandler("testJavascriptHandler", "{\"foo\":\"before ready\" }", newWVJBWebView.WVJBResponseCallback(){@Overridepublicvoidcallback(Objectdata){Toast.makeText(MainActivity.this, "Java call testJavascriptHandler got response! :" + data, Toast.LENGTH_LONG).show()} });...
- Copy and paste
setupWebViewJavascriptBridgeinto your JS:
functionsetupWebViewJavascriptBridge(callback){if(window.WebViewJavascriptBridge){returncallback(WebViewJavascriptBridge);}if(window.WVJBCallbacks){returnwindow.WVJBCallbacks.push(callback);}window.WVJBCallbacks=[callback];varWVJBIframe=document.createElement('iframe');WVJBIframe.style.display='none';WVJBIframe.src='wvjbscheme://__BRIDGE_LOADED__';document.documentElement.appendChild(WVJBIframe);setTimeout(function(){document.documentElement.removeChild(WVJBIframe)},0)}- Finally, call
setupWebViewJavascriptBridgeand then use the bridge to register handlers and call Java handlers:
setupWebViewJavascriptBridge(function(bridge){bridge.registerHandler('testJavascriptHandler',function(data,responseCallback){varresponseData={'Javascript Says':'Right back atcha!'}responseCallback(responseData)})varcallbackButton=document.getElementById('buttons').appendChild(document.createElement('button'))callbackButton.innerHTML='Fire testJavaCallback'callbackButton.onclick=function(e){bridge.callHandler('testJavaCallback',{'foo': 'bar'},function(response){log('JS got response',response)})}})- If you want to use
WebViewClient,you must use like this
publicclassCustomWebViewClientextendsWVJBWebViewClient{publicCustomWebViewClient(WVJBWebViewwebView){super(webView)} @OverridepublicbooleanshouldOverrideUrlLoading(WebViewview, Stringurl){// do your work here// ...returnsuper.shouldOverrideUrlLoading(view, url)} }- Read Demo for more detail.
Register a handler called handlerName. The javascript can then call this handler with WebViewJavascriptBridge.callHandler("handlerName").
Call the javascript handler called handlerName. If a WVJBResponseCallback callback is given the javascript handler can respond.
Register a handler called handlerName. The Java can then call this handler with public void callHandler(String handlerName, Object data) and public void callHandler(String handlerName, Object data,WVJBResponseCallback callback)
Example:
bridge.registerHandler("showAlert",function(data){alert(data)})bridge.registerHandler("getCurrentPageUrl",function(data,responseCallback){responseCallback(document.location.toString())})Call an Java handler called handlerName. If a responseCallback function is given the Java handler can respond.
Example:
bridge.callHandler("Log","Foo")bridge.callHandler("getScreenHeight",null,function(response){alert('Screen height:'+response)})