Zunächst möchte ich das beste Ergebnis auflisten, das ich abrufen konnte. jni call java method which take a custom java interface as parameterJNI Callback zu Java mit der Schnittstelle
Dies beantwortet meine nicht. Lass mich mein Problem erklären. Ich möchte NDK wie folgt anrufen.
(1) Java -> (2) CPP -> (3) C (neuer Thread) -> (4) CPP -> (5) Java
Code ist unten.
(1) Java
public interface Callback<T> {
void success(T result);
}
private native void jniUploadAsync(String imagePath, Callback<String> callback);
jniUploadAsync(file.getAbsolutePath(), new Callback<String>() {
@Override
public void success(final String result) {
Log.v("MyClass: result:: ", result);
}
});
(2) CPP
static JavaVM *jvm;
void imageUploadCallback(char *json, void *completionCallback) {
JNIEnv *env;
jint rs = jvm->AttachCurrentThread(&env, NULL);//create JNIEnv from JavaVM
jclass cbClass = env->FindClass("org/winster/test/Callback");
jmethodID method = env->GetMethodID(cbClass, "success", "(Ljava/lang/String;)V");
env->CallVoidMethod(static_cast<jobject>(completionCallback), method, "abcd");
}
void Java_org_winster_test_MyClass_jniUploadAsync(JNIEnv * env, jobject obj, jstring imagePath, jobject completionCallback) {
jint rs = env->GetJavaVM(&jvm); //Cache JavaVM here
CallMyCMethod((char *)filePath, &imageUploadCallback, &completionCallback);
}
(3) C
CallMyCMethod() //please assume that it works. The reason I need void* as the type for completionCallback is because, in ObjC implementation I use this
(4) CPP
//Call comes back to imageUploadCallback()
(5) Java
//I expect this Log.v("MyClass: result:: ", result); to be executed
Bitte beachten Sie, dass dies nicht eine grundlegende Frage, wie Java von C++ nennen. Die 2 spezifischen Punkte, die ich auflösen möchte, ist, wie man den "Callback" aufruft und wie man eine Methode in einer Java Interface Implementierung aufruft. Ich habe dies für Obj-C getan, wo es geradlinig ist.
und welchen Fehler haben Sie? –
Es ist unklar, woher 'env' in' imageUploadCallback' kommt. Es ist sehr wahrscheinlich, dass Sie 'AttachCurrentThread' aufrufen müssen. – Michael
JNI ERKENNUNG FEHLER IN ANWENDUNG: Verwendung ungültiger Jobject. Das ist der Fehler, den ich bekomme, wenn ich versuchte, den "Rückruf" – Winster