请问cocoscreator打包的web端游戏怎么与android交互?让android调起某个类里面的方法?
使用这个方法 jsb.reflection.callStaticMethod(className, methodName, methodSignature, “Title”, “Native Call Test”);
报错找不到jsb。
jsb不是打包成原生安卓才有的吗
那就是说web端不能交互了吗??
看浏览器是谁的啊 如果是自家的app + webview app端定义好接口就就行了
嗯嗯,已经解决了,蟹蟹。
我遇到了跟你相同的问题,能够在cocos的web项目中调用原生安卓的方法,但是安卓想要访问Web项目中的JS就一直失败,一直提示“test is not a function”,请问一下你是怎么解决的
同问 老哥你是怎么解决 安卓访问web 项目方法的?
postMessage
webview中js访问android方法
首先android中webview设置
webView.addJavascriptInterface(this, “android”);
webSettings.setJavaScriptEnabled(true);
这里的android可以替换成你想要字符串
然后在webview的activty中注册方法
@JavascriptInterface
public void test(String aaa){
Log.i(“test”,aaa);
}
只需要在java方法加上 @JavascriptInterface
在js 中调用
window.android.test(“aaa”);
这里建议判断下平台 和系统,判断方法是否存在
android调用js
在 js 中注册方法
window.test = function(aaa){
console.log(aaa);
}
在android中调用
webView.loadUrl(“javascript:if(window.test) {window.test( \“123\”);}”);
注意在主线程中调用
谢谢大佬~! 下午去试试
这个怎么写的呀,可以出一份代码吗?完全不懂
假如安卓app是你们自己的就可以这样做 安卓部分要你们的安卓工程人员写
/**
* 初始化cocos 往网页注入的方法
*这是游戏端需要先往window对象注入方法
*/
public static initGameLogic() {
if (this.getIsAndroid()) {
if (!window.android) {
window.android = {} as AndroidInterface
}
window.android.refreshBalance = () => {
EventManager.dispatch(E_AppNoticeEvent.e_refreshBalance)
}
window.android.joinBack = (isSuccess: boolean) => {
if (isSuccess) {
EventManager.dispatch(E_AppNoticeEvent.e_joinVoiceRoom)
} else {
this.showToast("JOIN VOICE FAIL", 2000)
}
}
window.android.speakUser = (userId: string) => {
EventManager.dispatch(E_AppNoticeEvent.e_speakerUserId, userId)
}
window.android.stopSpeakUser = (userId: string) => {
EventManager.dispatch(E_AppNoticeEvent.e_stopSpeakerUserId, userId)
}
window.android.isShowGame = (isShowGame: boolean) => {
this.log("安卓通知前后台:", isShowGame)
if (isShowGame) {
this.curIsBackground = false
this.showGame()
} else {
this.curIsBackground = true
this.hideGame()
}
}
} else {
if (!window.ios) {
window.ios = {} as IosInterface
}
window.ios.backToken = (token: string) => {
this.log("ios返回token", token)
this._playerToken = token
if (this.curIsBackground === false) {
SocketIO.toConnect()
}
}
window.ios.backIsBackground = (isBackground: string) => {
this.log("ios返回前后台", isBackground, typeof (isBackground))
if (Number(isBackground)) {
this.curIsBackground = true
} else {
this.curIsBackground = false
SocketIO.toConnect()
}
}
window.ios.refreshBalance = () => {
this.log("刷新余额")
EventManager.dispatch(E_AppNoticeEvent.e_refreshBalance)
}
window.ios.joinBack = (isSuccess: string) => {
this.log("加入房间是否成功", isSuccess)
if (Number(isSuccess)) {
EventManager.dispatch(E_AppNoticeEvent.e_joinVoiceRoom)
} else {
this.showToast("JOIN VOICE FAIL", 2000)
}
}
window.ios.speakUser = (userId: string) => {
EventManager.dispatch(E_AppNoticeEvent.e_speakerUserId, userId)
}
window.ios.stopSpeakUser = (userId: string) => {
EventManager.dispatch(E_AppNoticeEvent.e_stopSpeakerUserId, userId)
}
window.ios.isShowGame = (isShowGame: string) => {
this.log("ios通知前后台:", isShowGame)
if (Number(isShowGame)) {
//在苹果上进入多任务会通知进这里
if (this.curIsBackground) {
this.curIsBackground = false
this.showGame()
}
} else {
this.curIsBackground = true
this.hideGame()
}
}
}
this.log("Token:", this.getPlayerToken())
this.log("当前是前台:", this.getIsCanConnect())
this.log("SocketUrl:", this.getSocketIOUrl())
this.log("头像cdn地址:", this.getImgCdn())
this.log("是否是debug模式:", DEBUG)
this.log("是否是测试服Url:", this.getIsDebugEnv())
this.log("当前设备实际环境:", this.getPracticalPlatform())
this.log("游戏版本号:", this.getVersion())
this.log("是否有获取前后台方法:", Boolean(window.android && window.android.getIsBackground))
}
游戏客户端调用安卓方法更加简单了
window.android.starBGM(volume)
还可以获取返回值
哈哈,你给了我灵感,太感谢你了,我真聪明
我在web包里面声明了个 window.android = Android;然后在cocos 代码里面就可以直接调用 window[‘android’].onBackHome();与安卓进行交互啦
请问这种方式是需要手动修改 cocos 导出的 web-mobile 项目吗。我需要 Android 应用调用 Cocos,是不是需要 Android -> Web-Mobile -> Cocos 这样子,这样我需要在导出的 Web-Mobile 项目中定义一个方法接受 Android 的参数,然后再将参数传递到 Cocos 项目中吗
搞定了,把 cocos 项目当作 web 项目处理就可以了