I'm stuck with this, I need to call a Java function from c / C ++.
In the examples and tutorials, I see only a java application calling the c method, and in the same method calling another java method, but I want to call the java method from any part of the code. This is what I have:
static JNIEnv mEnv; static jclass mClassAndroidActivity; static mMethodSayHello; JNIEXPORT void JNICALL JNI_FUNCTION(AndroidActivity_nativeInit)(JNIEnv* env, jobject obj, int width, int height) { mEnv = env; jclass cls = (*env)->GetObjectClass(env, obj); mClassAndroidActivity = (*env)->NewGlobalRef(env, cls); mMethodSayHello = (*env)->GetMethodID (env, mClassAndroidActivity, "SayHello", "(Ljava/lang/String;)V"); } //this method is called from a cpp void nativeSayHello(char* msg) { jstring string = (*mEnv)->NewStringUTF(mEnv, msg); (*mEnv)->CallVoidMethod(mEnv, mClassAndroidActivity, mMethodSayHello, string); }
but always fail, I tried without NewGlobalRef, using mEnv instead of env in JNI_Function, I tried to get the method identifier from JNI_OnLoad, but always fail.
This is the journal I received:
02-15 18: 09: 48.520: W / dalvikvm (27904): JNI WARNING: threadid = 1, using env from threadid = 0
source share