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);

 

posted @ 2014-04-02 19:32  Marble  阅读(826)  评论(0编辑  收藏  举报