2013-01-19 66 views
0

我試圖讓Android DisplayMetrics類中使用NDK一個NativeActivity的應用程序。我想調用的函數是在這些線路的:通過JNI(安卓NDK)傳遞由C到Java對象(DisplayMetrics)

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

到目前爲止,我已經得到了目標設置和方法調用準備如下

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); 

不過,現在我不知道知道如何通過引用將度量jobject傳遞給display.getmetrics()函數以便操作系統調整它並正確填充它,以便我可以讀取其中的字段

回答

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