TS 和 Java 互相调用
https://codeleading.com/article/79462766348/
TS
1
| jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "CallJavaTest", "(Ljava/lang/String;Ljava/lang/String;)V","a","{b:1}");
|
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public static void CallJavaTest(final String id, final String jsonStr) { try { JSONObject tempData = JSONObject.parseObject(jsonStr); org.json.JSONObject properties = new org.json.JSONObject();
for (String key : tempData.keySet()) { properties.put(key, tempData.get(key)); } xxxxxx(id, properties);
} catch (Exception e) { e.printStackTrace(); } }
|
Java
1 2 3 4 5 6
| appActivity.runOnGLThread(new Runnable() { @Override public void run() { Cocos2dxJavascriptJavaBridge.evalString("window.GameJsb.testCallback("+ JSON.toJSONString(data) + ")"); } });
|
TS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| export default class GameJsb { private static _instance: GameJsb;
static get instance() { if (this._instance) { return this._instance; }
this._instance = new GameJsb(); return this._instance; }
testCallback(data) { console.log(JSON.stringify(data)); }
}
window["GameJsb"] = GameJsb.instance;
|