Home > Mobile >  Java jni callbacks error function for help
Java jni callbacks error function for help

Time:11-28

I use a udp so write their own
And then define the jni interface call so
In the udp receives the data is called recudp,
To call Java onProgressCallBack function in recudp
Code red is wrong place, at the callback when Java error

In the Java end onProgressCallBack are defined as follows

Public void onProgressCallBack (char [] data)
{
If (data!=null) {
TV. SetText (data, 0, the data length);
}
}



Part of the jni interface code is as follows:

JavaVM * g_VM;
Jobject g_obj;
Int recudp (char * data, int len)
{
JNIEnv * env.
//int getEnvStat=g_VM - & gt; The GetEnv (g_VM, (void * *) & amp; Env, JNI_VERSION_1_6);
Int getEnvStat=g_VM - & gt; The GetEnv () (void * * & amp; Env, JNI_VERSION_1_6);
If (getEnvStat==JNI_EDETACHED) {
//if not, take the initiative to attach to the JVM environment, access to the env
If (g_VM - & gt; AttachCurrentThread (& amp; Env, NULL)!=0) {
return -1;
}
}
//to be obtained through global variable g_obj callback class
Jclass javaClass=env - & gt; GetObjectClass (g_obj);
If (javaClass==0) {
G_VM - & gt; DetachCurrentThread ();
The return - 2;
}

//find a way to call ID
JmethodID javaCallback=env - & gt; GetMethodID (javaClass,
"OnProgressCallBack", "([C) V");
JcharArray da=env - & gt; NewCharArray (len);

Env - & gt; SetCharArrayRegion (da, 0, len, jchar * data);

//callback at last, the error code
env - & gt; CallIntMethod (g_obj javaCallback, da);

Env - & gt; DeleteLocalRef (da);
return 0;
}


Extern "C" JNIEXPORT void JNICALL
Java_com_example_udph264dec_MainActivity_InitUdp (
JNIEnv * env,
Jobject thiz/* this */,
Jint port) {
//JavaVM is a virtual machine in the JNI, said wait again another thread callback Java layer need to use
Env - & gt; GetJavaVM (& amp; G_VM);
//generates a global references preserved, so that the callback
G_obj=env - & gt; NewGlobalRef (thiz);

InitUDPServer (2048, recudp); So//start the udp code,
return ;
}

CodePudding user response:

1. Using the wrong callback methods CallIntMethod
Should be changed to CallVoidMethod

2. Should not be used in the callback function setText UI operation function, can only work on the UI thread

3. The inside of the Java char and char of c + + is an unequal is 8 bits should be changed to 16 a byte type
  • Related