Skip to content

maizuo-fe/bridge-android

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AndroidBridge

support callback function for bridge between javascript and Android

Usage

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

Api

AndroidBridge.setInvoker

  • param
    • object context
    • string invoker

example

AndroidBridge.setInvoker(appBindJs, 'passDataToJava');

AndroidBridge.invoke

  • param
    • any arguments
    • ...
    • function callback

example

AndroidBridge.invoke(1, {foo: 'bar'}, function (response) {
    console.log(response);
});

AndroidBridge.callHandler

  • 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');"); 

AndroidBridge.releaseInvoker

just for unit test exmaple

AndroidBridge.releaseInvoker();

About

the bridge connect android and javascript

Resources

License

Stars

Watchers

Forks

Packages

No packages published