[Android] Access the static enum field from JNI
In the world of Java Native Interface (JNI) on Android, we often need to access the field of Java class. But, the difficulties are we should understand what the code relationship between Java and JNI. In the previous post Programming Objects between Java and JNI, I’ve showed how to access the Java class that you are created. In this post, I will illustrate the method of accessing the Java enum class.
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
- Find the corresponding Java enum class
- Get the ID of the data field that you want to access
- Get the object by the data field id
Now, I will give a example of how to access the field “ARGB_8888″ of the android.graphics.Bitmap.Config,
Firstly, we see the definitions of android.graphics.Bitmap.Config. The Java class is contained four enum fields such as,
Enum Values | |
Bitmap.Config | ALPHA_8 |
Bitmap.Config | ARGB_4444 |
Bitmap.Config | ARGB_8888 |
Bitmap.Config | RGB_565 |
We want to access the field “ARGB_8888“, so we can program the following codes in the JNI.
jclass bitmapConfigClass = env->FindClass("android/graphics/Bitmap$Config");
jfieldID bitmapConfigField = env->GetStaticFieldID(bitmapConfigClass, "ARGB_8888", "Landroid/graphics/Bitmap$Config;");
jobject bitmapConfig = env->GetStaticObjectField(bitmapConfigClass, bitmapConfigField);
The key point is the second line. You must give the name of the field (ARGB_8888) that is showed in Java enum class. In addition, the fields are static, so we should use the GetStatic- Methods to get the resources in JNI.
發佈留言
很抱歉,必須登入網站才能發佈留言。