Cocos2dx--Cocos2dx与Android平台的跨平台调用

cocos2dx版本:cocos2dx2.2

示例下载地址:http://pan.baidu.com/s/1jGsT9C6


这里只是简单的说下如何调用非静态函数


c++调用java

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    {
        JniMethodInfo methodInfo;
		//判断有没有getObject()这个函数
        bool isHave = JniHelper::getStaticMethodInfo(methodInfo,
            "com/test/platformcall/PlatformCall",//需要调用的Java文件
            "getObject",//调用的方法名
            "()Ljava/lang/Object;");//参数

        jobject activityObj;
        if (isHave)
        {
			//获得一个实例对象,通过实例对象调用非静态函数
            activityObj = methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID);
        }

		//判断有没有say()这个函数
        isHave = JniHelper::getMethodInfo(methodInfo,
            "com/test/platformcall/PlatformCall",
            "say", //say()为非静态的函数
            "()V"); 
        if(isHave)
        {
// 			const char *pMessage = "keng die";//需要传递到Java层的字符串
// 			jstring StringArg = methodInfo.env->NewStringUTF(pMessage);
//          jint intA = 10;
//          jint intB = 20;
//          jboolean boolArg = true;
//          methodInfo.env->CallVoidMethod(jobj, methodInfo.methodID);
            methodInfo.env->CallVoidMethod(activityObj, methodInfo.methodID);
//             CCLog("result = %d", result);
        }
        else
        {
            CCLog("jni:the showMessage method is no exits");
        }
    }
#endif

java调用c++

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
extern
    "C"
{
	//android 调用 c++
    void Java_com_test_platformcall_PlatformCall_pause(JNIEnv*  env, jobject thiz,jint a)
    {
        CCLog("Android call C++ code, the tag is = %d", a);
    }
};
#endif


你可能感兴趣的:(android,jni,cocos2dx,跨平台,JniHelper)