一 : TS调用JAVA
window["WebViewJavascriptBridge"].callHandler(
‘enterGame‘
, JSON.stringify($param)
, function(responseData) {
// document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData
console.log( `enterGame 2 Android callback param : ${responseData}` );
}
);
PS 参数
①,JAVA的方法
②,JAVA的参数
③,回调函数
二 : JAVA调用TS
function connectWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) {
callback(WebViewJavascriptBridge)
} else {
document.addEventListener(
‘WebViewJavascriptBridgeReady‘
, function() {
callback(WebViewJavascriptBridge)
},
false
);
}
}
//调用方法
connectWebViewJavascriptBridge(function(bridge) {
bridge.init(function(message, responseCallback) {
console.log(‘JS got a message‘, message);
var data = {
‘Javascript Responds‘: ‘测试中文!‘
};
if (responseCallback) {
console.log(‘JS responding with‘, data);
responseCallback(data);
}
});
/// Java 调 JS
bridge.registerHandler("functionInJs", function(data, responseCallback) {
document.getElementById("show").innerHTML = ("data from Java: = " + data);
if (responseCallback) {
var responseData = "Javascript Says Right back aka!";
responseCallback(responseData);
}
});
})
注意 :
functionInJs : 注册JAVA可以调用的方法
原文地址:https://blog.51cto.com/aonaufly/2427861
时间: 2024-10-13 09:47:18