activity里有如下定义:
public void cppCall_nonStatic_logsth(){ //非静态方法 Log.i("cppCall_nonStatic", "test2~~~~!!!"); } public static Object cppCall_logsth(){ //静态方法 Log.i("cppCall", "test~~~~!!!"); return activity; }
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include#include #include #if 1 #define LOG_TAG "JniHelper" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) #else #define LOGD(...) #endif #endif
JniMethodInfo minfo; jobject jobj; bool b = JniHelper::getStaticMethodInfo(minfo, "com/loy/puzzles/Puzzles", //类路径 "cppCall_logsth", //静态方法名 "()Ljava/lang/Object;"); //括号里的是参数,后面的是返回值。 if (!b) { LOGD("JniHelper::getStaticMethodInfo error..."); }else{ jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID); } JniHelper::getMethodInfo(minfo, "com/loy/puzzles/Puzzles", "cppCall_nonStatic_logsth", "()V"); if(!b){ LOGD("JniHelper::getMethodInfo error..."); }else{ LOGD("ready to invoke method..."); minfo.env->CallVoidMethod(jobj, minfo.methodID); }
最终LOGCAT打印日志如下: 12-2 12:46:33.891: D/JniHelper(2008): testing~~~ 12-2 12:46:33.891: I/cppCall(2008): test~~~~!!! 12-2 12:46:33.891: D/JniHelper(2008): ready to invoke method... 12-2 12:46:33.891: I/cppCall_nonStatic(2008): test2~~~~!!! 12-2 12:46:33.891: D/JniHelper(2008): testing over~~~ 即表示调用成功。