JNIでスレッドを使用する方法

2355 ワード

      JNI          
JNI findClass Class, Class 。 GetEnv NULL

, , :)
:c++ Android API , , , , , , ,GetEnv 。 , :
If the current thread is not attached to the VM, sets *env to NULL, and returns JNI_EDETACHED. If the specified version is not supported, sets *env to NULL, and returns JNI_EVERSION. Otherwise, sets *env to the appropriate interface, and returns JNI_OK.
,the current thread is not attached。 AttachCurrentThread() DetachCurrentThread() 。 , gJvm->AttachCurrentThread(&env, NULL); env, , , findClass , , 。env , ? google , , ,google , bing , 。
class name , , 。 class name , class loader 。 class, , 。
jclass tmp = env->FindClass("com/example/company/MyClass");
myClass = (jclass)env->NewGlobalRef(tmp);

mid = env->GetStaticMethodID(cls, "fromJNI", "(I)V");
if (mid != NULL)
{
env->CallStaticVoidMethod(env, cls, mid, i);
}
, , 。 java c++ , 。

1. JNI_OnLoad , JavaVM*, , , JNIEnv* 。 , AttachCurrentThread env。
2. JavaVM* JNIEnv jclass。
3. jclass , 。
4. 。
5. , delete DetachCurrentThread 。