java交互_vue Java 交互

#JsBridge

inspired and modified from this and wechat jsBridge file, with some bugs fix and feature enhancement.

This project make a bridge between Java and JavaScript.

It provides safe and convenient way to call Java code from js and call js code from java.

I strongly recommend https://jitpack.io

repositories {//...maven { url"https://jitpack.io"}}dependencies {    compile'com.github.lzyzsd:jsbridge:1.0.4'}

add com.github.lzyzsd.jsbridge.BridgeWebView to your layout, it is inherited from WebView.

webView.registerHandler("submitFromWeb",newBridgeHandler() {@Overridepublicvoidhandler(Stringdata,CallBackFunctionfunction) {Log.i(TAG,"handler = submitFromWeb, data from web ="+data);            function.onCallBack("submitFromWeb exe, response data from Java");        }    });

js can call this Java handler method "submitFromWeb" through:

WebViewJavascriptBridge.callHandler('submitFromWeb', {'param':str1}        ,function(responseData) {document.getElementById("show").innerHTML="send get responseData from java, data ="+responseData        }    );

You can set a default handler in Java, so that js can send message to Java without assigned handlerName

webView.setDefaultHandler(newDefaultHandler());

window.WebViewJavascriptBridge.send(        data        ,function(responseData) {document.getElementById("show").innerHTML="repsonseData from java, data ="+responseData        }    );

WebViewJavascriptBridge.registerHandler("functionInJs",function(data,responseCallback) {document.getElementById("show").innerHTML=("data from Java: ="+data);varresponseData="Javascript Says Right back aka!";responseCallback(responseData);    });

Java can call this js handler function "functionInJs" through:

webView.callHandler("functionInJs",newGson().toJson(user),newCallBackFunction() {@OverridepublicvoidonCallBack(Stringdata) {        }    });

You can also define a default handler use init method, so that Java can send message to js without assigned handlerName

for example:

bridge.init(function(message,responseCallback) {console.log('JS got a message', message);vardata={'Javascript Responds':'Wee!'};console.log('JS responding with', data);responseCallback(data);    });

webView.send("hello");

will print 'JS got a message hello' and 'JS responding with' in webview console.

This lib will inject a WebViewJavascriptBridge Object to window object. So in your js, before use WebViewJavascriptBridge, you must detect if WebViewJavascriptBridge exist. If WebViewJavascriptBridge does not exit, you can listen to WebViewJavascriptBridgeReady event, as the blow code shows:

if(window.WebViewJavascriptBridge) {//do your work here}else{document.addEventListener('WebViewJavascriptBridgeReady',function() {//do your work here},false);    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值