vue Java 交互

https://github.com/lzyzsd/JsBridge README.md

#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.

Demo

Usage

JitPack.io

I strongly recommend https://jitpack.io

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

Use it in Java

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

Register a Java handler function so that js can call

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        }    );

Register a JavaScript handler function so that Java can call

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.

Notice

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);    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容