Get<type>Field Routines - GetFloatField
NativeType Get<type>Field
(JNIEnv *env, jobject obj,
jfieldID fieldID);
This family of accessor routines returns the value of an instance (nonstatic) field of an object. The field to access is specified by a field ID obtained by calling
GetFieldID()
. (GetFloatField)
The following table describes the Get<type>Field routine name and result type. You should replace type in Get<type>Field with the Java type of the field, or use one of the actual routine names from the table, and replace NativeType with the corresponding native type for that routine.
Table 4-1a Get<type>Field Family of Accessor Routines
LINKAGE:
Indices in the JNIEnv interface function table:
Table 4-1b Get<type>Field Family of Accessor Routines
PARAMETERS:
env
: the JNI interface pointer.obj
: a Java object (must not be NULL
).fieldID
: a valid field ID.RETURNS:
Returns the content of the field.
Example - GetFloatField
// 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(displayMetrics, 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(displayMetrics, height_id); JNI_ASSERT(jni, true); log("Height: ", height); // Height: 0 } // TODO: Delete objects here. app->activity->vm->DetachCurrentThread(); }