cocos2d-x与android通信
//.cpp private void callJava(const char* str) { #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo methodInfo; //getStaticMethodInfo获取到MethodId和ClassId,ClassID通过穿进去的Activity即类名获取,MethodId通过函数名获取 bool isHave = JniHelper::getStaticMethodInfo(methodInfo, "com/lengyuyue/cocos2dx/ForAndroid", "callJava", "(Ljava/lang/String;)V"); if(isHave) { jstring jdata = methodInfo.env->NewStringUTF(str); methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, jdata); } #else CCLog("called with android, not other platform"); #endif } extern "C" { //Java_包名_类名_方法名 void Java_com_lengyuyue_cocos2dx_ForAndroid_callCpp(JNIEnv* env, jobject object, jstring data) { const char* txt = env->GetStringUTFChars(data, false); CCLog("%s", txt); } }
//.java public static void callJava(String data) { System.out.println("call java‘s data = " + data); callCpp(data); } private static native void callCpp(String data);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。