1:NewGlobalRef
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_wkq_jnidemo_JniUtil_HancNetInit(JNIEnv *env, jclass clazz, jobject test) {
env->GetJavaVM(&jvm);
myTest = (jobject) env->NewGlobalRef(test);
back();
return HancNetSDK_Init();
}
2:GetObjectClass
jobject myTest;
static JavaVM *jvm;
void back() {
JNIEnv *env;
jvm->AttachCurrentThread(&env, NULL);
//找到接口类
jclass jSdkClass = env->GetObjectClass(myTest);
//找到接口类方法,第三个参数是方法签名
jmethodID javaCallback = env->GetMethodID(jSdkClass, "getX", "(Z)V");
//进行回调
env->CallVoidMethod(myTest, javaCallback, true);
}
3:最后注意方法前后顺序,被调用者代码一定在上面
jobject myTest;
static JavaVM *jvm;
void back() {
JNIEnv *env;
jvm->AttachCurrentThread(&env, NULL);
//找到接口类
jclass jSdkClass = env->GetObjectClass(myTest);
//找到接口类方法,第三个参数是方法签名
jmethodID javaCallback = env->GetMethodID(jSdkClass, "getX", "(Z)V");
//进行回调
env->CallVoidMethod(myTest, javaCallback, true);
}
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_wkq_jnidemo_JniUtil_HancNetInit(JNIEnv *env, jclass clazz, jobject test) {
env->GetJavaVM(&jvm);
myTest = (jobject) env->NewGlobalRef(test);
back();
return HancNetSDK_Init();
}
网友评论