在Android开发中,工作线程中是不能直接操作Main Thread,但在JNI开发时,回调接口时,是不知道由线程还是main thread触发。因此,需要在接口中作判断:
JNIEnv *env = NULL;
if ((*g_jvm)->GetEnv(g_jvm, (void *) &env, JNI_VERSION_1_6) == JNI_EDETACHED) {
// detached
(*env)->CallVoidMethod(env, g_obj, on_cb_id);
(*g_jvm)->DetachCurrentThread(g_jvm);
} else {
// attached
}
ref:
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html
Contentshttps://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html