Sunday, April 21, 2013

jni jnienv GetStaticObjectField example c c++ java


GetStatic<type>Field Routines - GetStaticObjectField


NativeType GetStatic<type>Field(JNIEnv *env, jclass clazz, jfieldID fieldID);

This family of accessor routines returns the value of a static field of an object. The field to access is specified by a field ID, which is obtained by calling GetStaticFieldID(). (GetStaticObjectField)

The following table describes the family of get routine names and result types. You should replace type in GetStatic<type>Field with the Java type of the field, or one of the actual static field accessor routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-5a GetStatic<type>Field Family of Accessor Routines

GetStatic<type>Field Routine Name

Native Type

GetStaticObjectField()

jobject

GetStaticBooleanField()

jboolean

GetStaticByteField()

jbyte

GetStaticCharField()

jchar

GetStaticShortField()

jshort

GetStaticIntField()

jint

GetStaticLongField()

jlong

GetStaticFloatField()

jfloat

GetStaticDoubleField()

jdouble
LINKAGE:
Indices in the JNIEnv interface function table.
Table 4-5b GetStatic<type>Field Family of Accessor Routines
GetStatic<type>Field Routine Name
Index
GetStaticObjectField()
145
GetStaticBooleanField()
146
GetStaticByteField()
147
GetStaticCharField()
148
GetStaticShortField()
149
GetStaticIntField()
150
GetStaticLongField()
151
GetStaticFloatField()
152
GetStaticDoubleField()
153
PARAMETERS:

env: the JNI interface pointer.

clazz: a Java class object.

fieldID: a static field ID.

RETURNS:

Returns the content of the static field.
Example of GetStaticObjectField

// "env" and "obj" are passed to a JNI function and are used unmodified in this code
// JNIEnv *env, jobject obj

jclass cls_context = NULL;
jclass cls_tm = NULL;
jobject tm = NULL;
jmethodID mid;
jfieldID fid;
jstring jstr;
jsize len_jstr;


cls_context = (*env)->FindClass(env, "android/content/Context");
fid = (*env)->GetStaticFieldID(env, cls_context, "TELEPHONY_SERVICE",
        "Ljava/lang/String;");
jstr = (*env)->GetStaticObjectField(env, cls_context, fid);

mid = (*env)->GetMethodID(env, cls_context, "getSystemService",
        "(Ljava/lang/String;)Ljava/lang/Object;");

tm = (*env)->CallObjectMethod(env, obj, mid, jstr);  // THIS LINE CRASHES

cls_tm = (*env)->FindClass(env, "android/telephony/TelephonyManager");

mid = (*env)->GetMethodID(env, cls_tm, "getDeviceId",
        "()Ljava/lang/String;");

jstr = (*env)->CallObjectMethod(env, tm, mid);

len_jstr = (*env)->GetStringUTFLength(env, jstr);

(*env)->GetStringUTFRegion(env, jstr, 0, len_jstr, buf_devid);