1

I have a scenario when my JNI is calling a Java method in order to display a dialog with two options, and the option should be passed back to the JNI method.

I think it should be some sort of a callback, but I have not idea how to approach it with C++.

Here is my code so far:

JniUtil.c

short GetOption(){
JNIEnv *env = GetEnv();
jclass clazz = (*env)->FindClass(env, "com/myapp/main/appJNI");
jmethodID getMethodId = (*env)->GetMethodID(env, clazz, "getSelectedOption", "()S");
jobject obj = GetEnvObject(MAIN_ACTIVITY_CLASS_ID);

jshort result = (*env)->CallShortMethod(env, obj, getMethodId);
return result;
}

appJNI.java

private short getSelectedOption(Context context) { 
AlertDialog alertDialog = new AlertDialog.Builder(context).create(); //Use context
alertDialog.setTitle("Title");
alertDialog.setMessage("What is your option?");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss(); 
                return 1;
            }
        });
alertDialog.show();
}

Problem is that the method must return a value, and not waiting for the dialog.

Palantiir
  • 55
  • 5
  • 1
    "I have a scenario when my JNI is calling a Java method in order to display a dialog with two options, and the option should be passed back to the JNI method" -- sorry, but that is not how Android's UI works. "I think it should be some sort of a callback" -- yes. "but I have not idea how to approach it with C++" -- add another exported JNI function, and call it once you have your input from the user. – CommonsWare Oct 28 '21 at 22:20

0 Answers0