Objective C使用JNI调用Java方法 [英] Objective C calling Java methods using JNI
问题描述
有人可以告诉我如何从Objective C调用Java方法。
Can someone show me how to call a Java method from Objective C.
更详细地说,这实际上是我想做的事情
In more detail this is actually what I would like to do
1)首先从java端调用目标C.在此调用期间我想获得对java对象的引用。
1) Have first call from the java side to the objective C. during this call I would like to get a reference to the java object.
2)稍后我希望Objective C使用上一步中获得的引用来调用Java方法。
2) Later down the line I would like Objective C to use the reference got in the previous step to call Java methods.
谢谢
推荐答案
以下似乎有效。它基于上面关于C示例和此链接的注释。
The following seems to work. It is based on the above comment on following the C examples and this link.
正如链接所说,不为env创建全局变量而是创建jvm的全局变量,同时创建对类的全局引用。
As the link says do not create a global variable for the env instead create a global variable of the jvm, also create a global reference to your class.
以下是我实现步骤1的方法:
1)在此调用期间,首先从java端调用目标C.我想获得对java对象的引用。
Here is how I implement the step 1 : " 1) Have first call from the java side to the objective C. during this call I would like to get a reference to the java object. "
首先在头文件中声明全局变量
First declare global variable in the header file for
1)jvm:
JavaVM *jvm;
2)Java类:
jclass smartCallbackClass;
3)java对象:
jobject smartCallbackObject;
接下来在Java端到Objective C的调用中设置这些变量的值
Next in the call that comes from the Java side to the Objective C set the values for these variables
1)对于JVM:
(*env)->GetJavaVM(env, &jvm);
2)对象:
smartCallbackObject = (*env)->NewGlobalRef(env, obj);
3)对于班级:
if (smartCallbackClass == NULL) {
jclass localRefCls = (*env)->FindClass(env,"com/studyisland/nativelibs/smart/responsesdk/interfaces/thin/SMARTResponseThinClient");
if (localRefCls == NULL) {
NSLog(@"Unable to create a JNI Java Class reference \n");
}
//Create a global reference for JNI Java class
smartCallbackClass = (*env)->NewGlobalRef(env,localRefCls);
//Delete the local reference as it is no longer needed
(*env)->DeleteLocalRef(env, localRefCls);
//Is the global reference created successfully?
if (smartCallbackClass == NULL) {
NSLog(@"Unable to create JNI Java class reference \n");
return 0;
}
}
这是我从中获取代码的链接班级
Here is the link from where I got the code for the class
http://java.sun.com/docs/books/jni/html/refs.html
现在第二步
2后来我希望Objective C使用上一步中获得的引用来调用Java方法
" 2) Later down the line I would like Objective C to use the reference got in the previous step to call Java methods "
要从Objective C调用回Java,你需要确保调用是在Java调用Objective C的同一个线程上完成的,所以这里是代码。
To call from Objective C back to Java you need to make sure that the call is done on the same thread by which Java called Objective C, so here is the code.
-(void)classFailedToStop:(SMARTResponseCallBackEventArg*)anArg{
JNIEnv *env;
int attach = (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL);
if(attach == 0){
(*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_4);
jmethodID method = (*env)->GetMethodID(env, smartCallbackClass, "callback_onStopClassFailed", "()V");
(*env)->CallVoidMethod(env, smartCallbackObject, method);
}
(*jvm)->DetachCurrentThread(jvm);
}
希望这会有所帮助。
这篇关于Objective C使用JNI调用Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!