使用Interface在JNI中实现回调函数

我需要使用“interface”在Java中实现回调函数。 我已经将应用程序部分编写为MyJavaFunction(int size, m_GetSizeInterface);

m_GetSizeInterface是一个包含回调函数GetSize的接口。 此GetSize方法在应用程序中被覆盖。 在JNI中,我需要调用一个具有原型int MyCPPFunction(int size, int (*callback)(int* ID));的CPP函数int MyCPPFunction(int size, int (*callback)(int* ID));

如何将此GetSize作为参数传递给JNI中的MyCPPFunction? 请帮忙

 public int GetSize (m_SizeClass arg0) { g_size = arg0.size; return 0; } 

这里的复杂之处在于您要调用本机C ++代码,而您又需要调用java方法。 这实际上有点棘手。

您需要为要调用的java创建一个JNI C ++函数,以及一个与MyCPPFunction回调签名匹配的C ++函数。 后者将充当调用java方法的包装器。

因为包装器需要有关JNI环境的信息,这些信息不能通过参数提供(以免破坏签名),所以您需要创建一些全局变量来保存它:

 jobject g_getSizeIface; jmethodID g_method; JNIEnv *g_env; 

java将调用的C ++函数如下:

 JNIEXPORT void JNICALL Java_ClassName_MyCPPFunction (JNIEnv *env, jint size, jobject getSizeInterface) { jclass objclass = env->GetObjectClass(getSizeInterface); jmethodID method = env->GetMethodID(objclass, "GetSize", "(m_SizeClass)I"); if(methodID == 0){ std::cout << "could not get method id!" << std::endl; return; } g_method = method; g_getSizeIface = getSizeInterface; g_env = env MyCPPFunction(size, WrapperFunc); } 

因此包装函数是:

 int WrapperFunc(int *id) { jint retval; //marshalling an int* to a m_SizeClass boogy-woogy. ... g_env->ExceptionClear(); retval = g_env->CallIntMethod(g_getSizeIface, g_method, /*marshalled m_SizeClass*/); if(g_env->ExceptionOccurred()){ //panic! Light fires! The British are coming!!! ... g_env->ExceptionClear(); } return rvalue; } 
 #include  #include  #include  class SimpleQueueEvent { public: SimpleQueueEvent(){}; ~SimpleQueueEvent(){}; //for C++ code call int queueEvent(std::function func){ int curTime = time(0) + rand() % 10000; eventMaps.insert(std::map>::value_type(curTime, func)); return curTime; //Call Java method to invoke method, such as //env->FindClass("...."); //jmethodID method = env->FindMethod("onPostQueueEvent"...); //env->InvokeVoidMethod(); //Java code like this.. // private void onPostQueueEvent(final int eventId){ // listener.PostQueueEvent(new Runnable() { // public void run() { // nativeEventFunc(eventId); // } // }); // private static native void nativeEventFunc(int eventId); } void nativeEventFunc(int eventId){ if(eventMaps.find(eventId) != eventMaps.end()){ std::function func = eventMaps.at(eventId); func(); } } private: std::map> eventMaps; }; 

//并且测试代码是:

  SimpleQueueEvent queueEvent; std::function func = [](){ printf("native runnable..\n"); }; int evenId = queueEvent.queueEvent(func); queueEvent.nativeEventFunc(evenId);