简体   繁体   中英

Pass an object (DisplayMetrics) from C to Java through JNI (Android NDK)

I am trying to get the Android DisplayMetrics class in a NativeActivity app using the NDK. The functions I am trying to call are the ones in these lines:

DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = this.getWindowManager();
Display display = wm.getDefaultDisplay();
display.getMetrics(metrics);

So far, I have gotten the objects setup and the method calls ready as follows

JNIEnv *jni; 
state->activity->vm->AttachCurrentThread(&jni, NULL);
jclass activityClass = jni->FindClass("android/app/NativeActivity");
jclass windowManagerClass = jni->FindClass("android/view/WindowManager");
jclass displayClass = jni->FindClass("android/view/Display");
jclass displayMetricsClass = jni->FindClass("android/util/DisplayMetrics");

jmethodID getWindowManager = jni->GetMethodID(activityClass, "getWindowManager", "()Landroid/view/WindowManager;");
jmethodID getDefaultDisplay = jni->GetMethodID(windowManagerClass, "getDefaultDisplay", "()Landroid/view/Display;");
jmethodID getMetrics = jni->GetMethodID(displayClass, "getMetrics", "(android/util/DisplayMetrics)L;");
jmethodID newDisplayMetrics = jni->GetMethodID(displayMetricsClass, "DisplayMetrics", "()Landroid/util/DisplayMetrics;");

jobject metrics = jni->CallObjectMethod(displayMetricsClass, newDisplayMetrics);
jobject windowManager = jni->CallObjectMethod(state->activity->clazz, getWindowManager);
jobject display = jni->CallObjectMethod(windowManager, getDefaultDisplay);

However, now I do not know how to pass in the metrics jobject to the display.getmetrics() function by reference in order to have the OS adjust it and fill it up correctly, so I can read the fields in it

jmethodID getMetrics = jni->GetMethodID(displayClass, "getMetrics", "(Landroid/util/DisplayMetrics;)V");
jni->CallVoidMethod(display, getMetrics, metrics);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM