我们正在使用NativeActivity类在 android 上开发 OpenGL 游戏。到目前为止一切正常,但现在我们需要访问一些似乎只能从 Java 获得的功能。
还有更多,但我们认为有用的第一个是访问显示 DPI。如此处所述,Java 代码如下所示:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
这是不幸的相应 C++ 代码:
// My checking routine.
#define JNI_ASSERT(jni, cond) { \
if (!(cond)) {\
std::stringstream ss; \
ss << __FILE__ << ":" << __LINE__; \
throw std::runtime_error(ss.str()); \
} \
if (jni->ExceptionCheck()) { \
std::stringstream ss; \
ss << __FILE__ << ":" << __LINE__; \
throw std::runtime_error("Exception: " + ss.str()); \
} \
}
void print_dpi(android_app* app) {
JNIEnv* jni;
app->activity->vm->AttachCurrentThread(&jni, NULL);
jclass activityClass = jni->FindClass("android/app/NativeActivity");
JNI_ASSERT(jni, activityClass);
jmethodID getWindowManager = jni->GetMethodID
( activityClass
, "getWindowManager"
, "()Landroid/view/WindowManager;");
JNI_ASSERT(jni, getWindowManager);
jobject wm = jni->CallObjectMethod(app->activity->clazz, getWindowManager);
JNI_ASSERT(jni, wm);
jclass windowManagerClass = jni->FindClass("android/view/WindowManager");
JNI_ASSERT(jni, windowManagerClass);
jmethodID getDefaultDisplay = jni->GetMethodID( windowManagerClass
, "getDefaultDisplay"
, "()Landroid/view/Display;");
JNI_ASSERT(jni, getDefaultDisplay);
jobject display = jni->CallObjectMethod(wm, getDefaultDisplay);
JNI_ASSERT(jni, display);
jclass displayClass = jni->FindClass("android/view/Display");
JNI_ASSERT(jni, displayClass);
// Check if everything is OK so far, it is, the values it prints
// are sensible.
{
jmethodID getWidth = jni->GetMethodID(displayClass, "getWidth", "()I");
JNI_ASSERT(jni, getWidth);
jmethodID getHeight = jni->GetMethodID(displayClass, "getHeight", "()I");
JNI_ASSERT(jni, getHeight);
int width = jni->CallIntMethod(display, getWidth);
JNI_ASSERT(jni, true);
log("Width: ", width); // Width: 320
int height = jni->CallIntMethod(display, getHeight);
JNI_ASSERT(jni, true);
log("Height: ", height); // Height: 480
}
jclass displayMetricsClass = jni->FindClass("android/util/DisplayMetrics");
JNI_ASSERT(jni, displayMetricsClass);
jmethodID displayMetricsConstructor = jni->GetMethodID( displayMetricsClass
, "<init>", "()V");
JNI_ASSERT(jni, displayMetricsConstructor);
jobject displayMetrics = jni->NewObject( displayMetricsClass
, displayMetricsConstructor);
JNI_ASSERT(jni, displayMetrics);
jmethodID getMetrics = jni->GetMethodID( displayClass
, "getMetrics"
, "(Landroid/util/DisplayMetrics;)V");
JNI_ASSERT(jni, getMetrics);
jni->CallVoidMethod(display, getMetrics, displayMetrics);
JNI_ASSERT(jni, true);
{
jfieldID xdpi_id = jni->GetFieldID(displayMetricsClass, "xdpi", "F");
JNI_ASSERT(jni, xdpi_id);
float xdpi = jni->GetFloatField(displayMetricsClass, xdpi_id);
JNI_ASSERT(jni, true);
log("XDPI: ", xdpi); // XDPI: 0
}
{
jfieldID height_id = jni->GetFieldID( displayMetricsClass
, "heightPixels", "I");
JNI_ASSERT(jni, height_id);
int height = jni->GetIntField(displayMetricsClass, height_id);
JNI_ASSERT(jni, true);
log("Height: ", height); // Height: 0
}
// TODO: Delete objects here.
app->activity->vm->DetachCurrentThread();
}
代码输出:
Width: 320
Height: 480
XDPI: 0
Height: 0
就好像displayMetrics对象没有在调用中设置
jni->CallVoidMethod(display, getMetrics, displayMetrics);
JNI 是否不允许我使用参数作为返回值?如果是这样,鉴于我们使用的是 NativeActivity 胶水,我们该如何解决它。