线程由C层pthread启动的,需要DetachCurrentThread。
JNIEnv *env; g_jvm->AttachCurrentThread(&env, NULL); //必须AttachCurrentThread,env只能在自己的线程运行 jclass clazz = getObjectClass(env, g_monitorObj); jmethodID onLoginedMethod = getMethodID(env, clazz,"onLogStateChged","(ILjava/lang/String;)V"); jstring deviceId = (env)->NewStringUTF(TargDeviceID); env->CallVoidMethod(g_monitorObj, onLoginedMethod, CurState, deviceId); env->DeleteLocalRef(deviceId); g_jvm->DetachCurrentThread();
若由Java启动的,不需要DetachCurrentThread。
JNIEnv *env; g_jvm->AttachCurrentThread(&env, NULL); //必须AttachCurrentThread,env只能在自己的线程运行 jclass clazz = getObjectClass(env, g_monitorObj); jmethodID onLoginedMethod = getMethodID(env, clazz,"onLogStateChged","(ILjava/lang/String;)V"); jstring deviceId = (env)->NewStringUTF(TargDeviceID); env->CallVoidMethod(g_monitorObj, onLoginedMethod, CurState, deviceId); env->DeleteLocalRef(deviceId); //g_jvm->DetachCurrentThread();