Skip to content

Commit 9894bc2

Browse files
author
lemon
committed
支持JavaScript通过bridge桥注册可供Java调用的函数
1 parent 26220df commit 9894bc2

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

app/src/main/assets/demo.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
document.addEventListener('WebViewJavascriptBridgeReady',
1212
function () {
1313
toast('inject finished');
14-
window['easyBridge'].resultBack = function (parameters, callback) {
14+
window.easyBridge.registerHandler('resultBack', function (parameters, callback) {
1515
if (typeof callback == 'function') {
1616
callback(parameters);
1717
}
18-
};
18+
});
1919
}, false);
2020

2121
function toast(params = 'toast data') {
22-
window['easyBridge'].callHandler('toast', 'toast with:' + params + 'at ' + new Date().toLocaleString());
22+
window.easyBridge.callHandler('toast', 'toast with:' + params + 'at ' + new Date().toLocaleString());
2323
}
2424
</script>
2525
</head>

easybridge/src/main/assets/easybridge.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
parameters = args || {};
3232
parameters = JSON.stringify(parameters);
3333
}
34-
//the name '_easybridge' is an java object that mapping to a javascript onject,using addJavaInterface in Java code
34+
//the name '_easybridge' is an java object that mapping to a javascript object,using addJavaInterface in Java code
3535
if (window._easybridge) {
3636
setTimeout(function () {
3737
//the function enqueue is the pubic method from Java Code,using to call Java logic
@@ -42,6 +42,11 @@
4242
}
4343
}
4444

45+
//the function for JavaScript to register handler that Java Code Can Executed
46+
function registerHandler(handlerName, handler) {
47+
window[bridgeName][handlerName] = handler;
48+
}
49+
4550
//the function for Java Code to invoked JavaScript function
4651
function _executeScript(handlerName, parameters, callbackId) {
4752
if (!handlerName) {
@@ -104,6 +109,7 @@
104109
//init the bridge object
105110
window[bridgeName] = {
106111
callHandler: callHandler,
112+
registerHandler: registerHandler,
107113
_executeScript: _executeScript,
108114
_dispatchResult: _dispatchResult,
109115

0 commit comments

Comments
 (0)