1.接入?
a.在最外層build.gradle添加 allprojects {repositories {jcenter()maven{url "https://jitpack.io"}}}?
b.在主工程build.gradle添加 dependencies{compile 'com.github.lzyzsd:jsbridge:1.0.4' }
?2.使用
?A.在布局文件中使用BridgeWebView替代WebView?
B.js調(diào)用java?
場(chǎng)景一:自定義函數(shù)調(diào)用廉白,調(diào)用指定的函數(shù),js端與java端的方法名需保持一致
?1)java端注冊(cè)handler,實(shí)現(xiàn)js調(diào)用的回調(diào)?
?BridgeWebView mBridgeWebView = (BridgeWebView)context.findViewById(R.id.xxx); mBridgeWebView.registerHandler(?
?//第一個(gè)參數(shù)與js端定義的方法名保持一致
?"myJsThodName",?
?//第二個(gè)參數(shù)BridgeHandler,接收js傳來(lái)的數(shù)據(jù)并將處理結(jié)果回調(diào)給js端?
?new BridgeHandler(){@overridepublic void handler(String data ,CallbackFunction function){Log.e(TAG,"js端傳來(lái)的數(shù)據(jù)是:"+data);
function.onCallBack("回調(diào)內(nèi)容");} } );
?2)js端定義調(diào)用java的函數(shù)
調(diào)用java
function javaClick(){
var data="給java端數(shù)據(jù)";
window.WebViewJavascriptBridge.callHandler('myJsThodName',
data,function (responseData){birdgeLog('來(lái)自java端回傳的數(shù)據(jù)'+responseData);});? }? ? ??
場(chǎng)景二:默認(rèn)函數(shù)調(diào)用,無(wú)需額外定義方法名??
1)java端設(shè)置默認(rèn)handler? mBridgeWebView.setDefaultHandler(new BridgeHandler(){@overridepublic void handler(String data,CallbackFunction function){Log.e(TAG,"js端傳來(lái)的數(shù)據(jù)是:"+data);function.onCallBack("回調(diào)內(nèi)容);}}? );? ? ? 2)js端定義調(diào)用java的函數(shù)
調(diào)用java用default方式
function javaClick(){var data = "給java端的數(shù)據(jù)"window.WebViewJavascriptBridge.send(data,function (responseData){birdgeLog('來(lái)自java端回傳的數(shù)據(jù)'+responseData);});? }? ??
C. java調(diào)用js?
****js端代碼****
1)注冊(cè)事件監(jiān)聽(tīng)function connectWebViewJavascriptBridge(callback){
//判斷WebViewJavascriptBridge是否存在端三,不存在需要通過(guò)webViewJavascriptBridgeReady來(lái)添加監(jiān)聽(tīng)
if(window.WebViewJavascriptBridge){callback(WebViewJavascriptBridge);}
else{document.addEventListeter('WebViewJavascriptBridgeReady',
function(){callback(WebViewJavascriptBridge);},false);}}
2)注冊(cè)回調(diào)函數(shù),初始化函數(shù)connectWebViewJavascriptBridge(function(bridge){
//場(chǎng)景一:默認(rèn)函數(shù)調(diào)用
bridge.init(function(data,responseCallback){bridgeLog('java端發(fā)來(lái)的數(shù)據(jù)'+data);
var responseData = '回傳給java端的數(shù)據(jù)';
responseCallback(responseData);});
//場(chǎng)景二:自定義函數(shù)調(diào)用bridge.registerHandler(
//與java端方法名保持一致
'myJavaThodName',function(data,responseCallback){bridgeLog('java端發(fā)來(lái)的數(shù)據(jù)'+data);
var responseData = '回傳給java端的數(shù)據(jù)';responseCallback(responseData);});})
js共通函數(shù),log打印函數(shù)
顯示web打印日志
function bridgeLog(logContent) {? ? ? ? ? document.getElementById("log_msg").innerHTML = logContent;? ? ? }
******java端代碼*****
場(chǎng)景一:默認(rèn)函數(shù)調(diào)用mBridgeWebView.send("給js端發(fā)送的數(shù)據(jù)",new CallbackFunction(){@overridepublic void onCallBack(String data){Log.e(TAG,"js端回傳的數(shù)據(jù)"+data);}});
場(chǎng)景二:自定義函數(shù)調(diào)用mBridgeWebView.callHandler(){"myJavaThodName","給js端發(fā)送的數(shù)據(jù)",new CallbackFunction(){@overridepublic void onCallBack(String data){Log.e(TAG,"js端回傳的數(shù)據(jù)"+data);}}}