示例代码段2:
Xcode 代码:
-
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
- JniMethodInfo minfo;
-
- bool isHave = JniHelper::getStaticMethodInfo(minfo,
- "com/ai/ommr/OhMonsterMR4Android", "testFunWithInt","(I)V");
-
- if (!isHave) {
- CCLog("jni:此函数不存在");
- }else{
- CCLog("jni:此函数存在");
-
- minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID,823);
- }
- CCLog("jni-java函数执行完毕");
- #endif
Android(Java) 代码:
-
- public static void testFunWithInt(int _int) {
- Log.e("Himi", "静态函数示例1.有参数,无返回值;传入的参数int=" + _int);
- }
运行截图:
示例代码段3:
Xcode 代码:
-
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
- JniMethodInfo minfo;
-
-
- bool isHave = JniHelper::getStaticMethodInfo(minfo,
- "com/ai/ommr/OhMonsterMR4Android","testFunWithIntAndRtn","(I)I");
- jint _int ;
- if (!isHave) {
- CCLog("jni:此函数不存在");
- }else{
- CCLog("jni:此函数存在");
-
- _int = minfo.env->CallStaticIntMethod(minfo.classID, minfo.methodID,823);
-
-
- JniMethodInfo minfo_ty;
- bool isHave = JniHelper::getStaticMethodInfo(minfo_ty, "com/ai/ommr/OhMonsterMR4Android", "testFunWithInt", "(I)V");
- if (isHave) {
- minfo_ty.env->CallStaticVoidMethod(minfo_ty.classID, minfo_ty.methodID,_int);
- }
-
- }
- CCLog("jni-java函数执行完毕");
- #endif
Android(Java) 代码:
-
- public static int testFunWithIntAndRtn(int _int) {
- Log.e("Himi", "静态函数示例1.有参数,有返回值;传入的参数int=" + _int);
- return _int+1000;
- }
运行截图:
示例代码段4:
Xcode 代码:
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
- JniMethodInfo minfo;
-
- bool isHave = JniHelper::getStaticMethodInfo(minfo, "com/ai/ommr/OhMonsterMR4Android","testFunWithStringAndRtn","(I)Ljava/lang/String;");
- jobject jobj;
- if (!isHave) {
- CCLog("jni:此函数不存在");
- }else{
- CCLog("jni:此函数存在");
-
- jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,823);
- }
- CCLog("jni-java函数执行完毕");
- #endif
Android(Java) 代码:
-
- public static String testFunWithStringAndRtn(int _int) {
- Log.e("Himi", "静态函数示例4.有参数,有返回值(String类型);int=" + _int);
- return "yes,return 'String' is OK --by Himi";
- }
运行截图:
示例代码段5:
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
- JniMethodInfo minfo;
-
- bool isHave = JniHelper::getStaticMethodInfo(minfo,
- "com/ai/ommr/OhMonsterMR4Android",
- "rtnActivity",
- "()Ljava/lang/Object;");
- jobject jobj;
- if (isHave) {
- jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);
- }
- CCLog("正确获取到 jobj");
-
-
- isHave = JniHelper::getMethodInfo(minfo,
- "com/ai/ommr/OhMonsterMR4Android",
- "nostaticFun",
- "()V");
-
- if (isHave) {
- minfo.env->CallVoidMethod(jobj, minfo.methodID);
- }
- CCLog("jni-java函数执行完毕");
- #endif
Java 代码:
-
-
- public static Activity actInstance;
- public static Object rtnActivity() {
- return actInstance;
- }
-
- public void nostaticFun() {
- Log.e("Himi", "no static Function is OK - By Himi");
- }
本文出自 “李华明Himi” 博客,请务必保留此出处http://xiaominghimi.blog.51cto.com/2614927/908818