support callback function for bridge between javascript and Android
setup Java:
public void passDataToApp(String type, String dataJson, String handlerName) {
String result = "result here";
// ...
webView.loadUrl("javascript:AndroidBridge.callHandler('" + handlerName + "', '" + result + "');");
}
setup Javascript:
(function () {
var AndroidBridge = window.AndroidBridge;
AndroidBridge.setInvoker(appBindJs, 'passDataToJava');
})();
then invoke from Javascript:
(function () {
var AndroidBridge = window.AndroidBridge;
var type = '1';
var data = {foo: 'bar'};
AndroidBridge.invoke(type, data, function (response) {
console.log(response);
});
})();
- param
- object context
- string invoker
example
AndroidBridge.setInvoker(appBindJs, 'passDataToJava');
- param
- any arguments
- ...
- function callback
example
AndroidBridge.invoke(1, {foo: 'bar'}, function (response) {
console.log(response);
});
- param
- string handlerName
- any arguments
- ...
exmaple
AndroidBridge.callHandler('1399358347906v1EoKltD', 'response here');
in java:
String result = "response here";
webView.loadUrl("javascript:AndroidBridge.callHandler('" + handlerName + "', '" + result + "');");
handlers can receive arguments with any length. e.g.:
webView.loadUrl("javascript:AndroidBridge.callHandler('" + handlerName + "', 'foo', 'bar', 'baz');");
just for unit test exmaple
AndroidBridge.releaseInvoker();