I have learned that the JNI interface pointer (JNIEnv *) is only valid in the current thread. Suppose I started a new thread inside a native method; how it can asynchronously send events to a Java method? As this new thread can’t have a reference of (JNIEnv *). Storing a global variable for (JNIEnv *) apparently will not work?
Share
Within synchronous calls using JNI from Java to C++ the "environment" has already been setup by the JVM, however going in the other direction from an arbitrary C++ thread it may not have been
Therefore you need to follow these steps
GetEnvAttachCurrentThreadCallVoidMethodDetachCurrentThreadFull example. Note I have written about this in the past in more detail on my blog