X5Bridge
The library of the bridge between Android native and JavaScript
Other communication interactions
Google native has a set of communication, in fact, it can also complete callback interaction, mainly through interface annotation. But JavaScript needs to define the global method
Usage
JitPack.io
Use it in Java
add com.x5bridgelibrary.jsbridge.BridgeWebViewto your layout, it is inherited from WebView.
Register a Java handler function so that js can call
js can call this Java handler method "submitFromWeb" through:
You can set a default handler in Java, so that js can send message to Java without assigned handlerName
Register a JavaScript handler function so that Java can call
Java can call this js handler function "functionInJs" through:
You can also define a default handler use init method, so that Java can send message to js without assigned handlerName
for example:
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: