JAVA calls JS
//Java端
//带参数的函数调用
final String exes = "cc.Global.setEcADVal('"+ var1 + "')";
//必须在GL线程中进行
Cocos2dxHelper.runOnGLThread(new Runnable() {
@Override
public void run() {
//注意这里构造了带参数的函数
Cocos2dxJavascriptJavaBridge.evalString(exes);
}
});
//js端
setEcADVal(jsonVal) {
}
JS calls Java
//js端
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Test", "hello","(Z)V", true)});
//java端
public static void Test(boolean isVisibility) {
}
Possible causes of android.os.DeadObjectException:
- There may be uninstanced variables in the java code, so check the java code first.
- It may also be a permission problem, try to add the following permissions in AndroidMainfest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
- When java calls js, the engine may not be initialized. The way to solve this error is to confirm that the engine has been initialized in the js code (such as a delay of a few seconds): first use js to call java, and then use java to call js. This will avoid errors.
Attach the details of mutual calls between Java and JS: link