NDK(12)Jni常用函数
1.参考官方文档
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html
3.函数列表
1 typedef const struct JNINativeInterface *JNIEnv; 2 const struct JNINativeInterface ... = { 3 4 NULL, 5 NULL, 6 NULL, 7 NULL, 8 GetVersion, 9 10 DefineClass, 11 FindClass, 12 13 FromReflectedMethod, 14 FromReflectedField, 15 ToReflectedMethod, 16 17 GetSuperclass, 18 IsAssignableFrom, 19 20 ToReflectedField, 21 22 Throw, 23 ThrowNew, 24 ExceptionOccurred, 25 ExceptionDescribe, 26 ExceptionClear, 27 FatalError, 28 29 PushLocalFrame, 30 PopLocalFrame, 31 32 NewGlobalRef, 33 DeleteGlobalRef, 34 DeleteLocalRef, 35 IsSameObject, 36 NewLocalRef, 37 EnsureLocalCapacity, 38 39 AllocObject, 40 NewObject, 41 NewObjectV, 42 NewObjectA, 43 44 GetObjectClass, 45 IsInstanceOf, 46 47 GetMethodID, 48 49 CallObjectMethod, 50 CallObjectMethodV, 51 CallObjectMethodA, 52 CallBooleanMethod, 53 CallBooleanMethodV, 54 CallBooleanMethodA, 55 CallByteMethod, 56 CallByteMethodV, 57 CallByteMethodA, 58 CallCharMethod, 59 CallCharMethodV, 60 CallCharMethodA, 61 CallShortMethod, 62 CallShortMethodV, 63 CallShortMethodA, 64 CallIntMethod, 65 CallIntMethodV, 66 CallIntMethodA, 67 CallLongMethod, 68 CallLongMethodV, 69 CallLongMethodA, 70 CallFloatMethod, 71 CallFloatMethodV, 72 CallFloatMethodA, 73 CallDoubleMethod, 74 CallDoubleMethodV, 75 CallDoubleMethodA, 76 CallVoidMethod, 77 CallVoidMethodV, 78 CallVoidMethodA, 79 80 CallNonvirtualObjectMethod, 81 CallNonvirtualObjectMethodV, 82 CallNonvirtualObjectMethodA, 83 CallNonvirtualBooleanMethod, 84 CallNonvirtualBooleanMethodV, 85 CallNonvirtualBooleanMethodA, 86 CallNonvirtualByteMethod, 87 CallNonvirtualByteMethodV, 88 CallNonvirtualByteMethodA, 89 CallNonvirtualCharMethod, 90 CallNonvirtualCharMethodV, 91 CallNonvirtualCharMethodA, 92 CallNonvirtualShortMethod, 93 CallNonvirtualShortMethodV, 94 CallNonvirtualShortMethodA, 95 CallNonvirtualIntMethod, 96 CallNonvirtualIntMethodV, 97 CallNonvirtualIntMethodA, 98 CallNonvirtualLongMethod, 99 CallNonvirtualLongMethodV, 100 CallNonvirtualLongMethodA, 101 CallNonvirtualFloatMethod, 102 CallNonvirtualFloatMethodV, 103 CallNonvirtualFloatMethodA, 104 CallNonvirtualDoubleMethod, 105 CallNonvirtualDoubleMethodV, 106 CallNonvirtualDoubleMethodA, 107 CallNonvirtualVoidMethod, 108 CallNonvirtualVoidMethodV, 109 CallNonvirtualVoidMethodA, 110 111 GetFieldID, 112 113 GetObjectField, 114 GetBooleanField, 115 GetByteField, 116 GetCharField, 117 GetShortField, 118 GetIntField, 119 GetLongField, 120 GetFloatField, 121 GetDoubleField, 122 SetObjectField, 123 SetBooleanField, 124 SetByteField, 125 SetCharField, 126 SetShortField, 127 SetIntField, 128 SetLongField, 129 SetFloatField, 130 SetDoubleField, 131 132 GetStaticMethodID, 133 134 CallStaticObjectMethod, 135 CallStaticObjectMethodV, 136 CallStaticObjectMethodA, 137 CallStaticBooleanMethod, 138 CallStaticBooleanMethodV, 139 CallStaticBooleanMethodA, 140 CallStaticByteMethod, 141 CallStaticByteMethodV, 142 CallStaticByteMethodA, 143 CallStaticCharMethod, 144 CallStaticCharMethodV, 145 CallStaticCharMethodA, 146 CallStaticShortMethod, 147 CallStaticShortMethodV, 148 CallStaticShortMethodA, 149 CallStaticIntMethod, 150 CallStaticIntMethodV, 151 CallStaticIntMethodA, 152 CallStaticLongMethod, 153 CallStaticLongMethodV, 154 CallStaticLongMethodA, 155 CallStaticFloatMethod, 156 CallStaticFloatMethodV, 157 CallStaticFloatMethodA, 158 CallStaticDoubleMethod, 159 CallStaticDoubleMethodV, 160 CallStaticDoubleMethodA, 161 CallStaticVoidMethod, 162 CallStaticVoidMethodV, 163 CallStaticVoidMethodA, 164 165 GetStaticFieldID, 166 167 GetStaticObjectField, 168 GetStaticBooleanField, 169 GetStaticByteField, 170 GetStaticCharField, 171 GetStaticShortField, 172 GetStaticIntField, 173 GetStaticLongField, 174 GetStaticFloatField, 175 GetStaticDoubleField, 176 177 SetStaticObjectField, 178 SetStaticBooleanField, 179 SetStaticByteField, 180 SetStaticCharField, 181 SetStaticShortField, 182 SetStaticIntField, 183 SetStaticLongField, 184 SetStaticFloatField, 185 SetStaticDoubleField, 186 187 NewString, 188 189 GetStringLength, 190 GetStringChars, 191 ReleaseStringChars, 192 193 NewStringUTF, 194 GetStringUTFLength, 195 GetStringUTFChars, 196 ReleaseStringUTFChars, 197 198 GetArrayLength, 199 200 NewObjectArray, 201 GetObjectArrayElement, 202 SetObjectArrayElement, 203 204 NewBooleanArray, 205 NewByteArray, 206 NewCharArray, 207 NewShortArray, 208 NewIntArray, 209 NewLongArray, 210 NewFloatArray, 211 NewDoubleArray, 212 213 GetBooleanArrayElements, 214 GetByteArrayElements, 215 GetCharArrayElements, 216 GetShortArrayElements, 217 GetIntArrayElements, 218 GetLongArrayElements, 219 GetFloatArrayElements, 220 GetDoubleArrayElements, 221 222 ReleaseBooleanArrayElements, 223 ReleaseByteArrayElements, 224 ReleaseCharArrayElements, 225 ReleaseShortArrayElements, 226 ReleaseIntArrayElements, 227 ReleaseLongArrayElements, 228 ReleaseFloatArrayElements, 229 ReleaseDoubleArrayElements, 230 231 GetBooleanArrayRegion, 232 GetByteArrayRegion, 233 GetCharArrayRegion, 234 GetShortArrayRegion, 235 GetIntArrayRegion, 236 GetLongArrayRegion, 237 GetFloatArrayRegion, 238 GetDoubleArrayRegion, 239 SetBooleanArrayRegion, 240 SetByteArrayRegion, 241 SetCharArrayRegion, 242 SetShortArrayRegion, 243 SetIntArrayRegion, 244 SetLongArrayRegion, 245 SetFloatArrayRegion, 246 SetDoubleArrayRegion, 247 248 RegisterNatives, 249 UnregisterNatives, 250 251 MonitorEnter, 252 MonitorExit, 253 254 GetJavaVM, 255 256 GetStringRegion, 257 GetStringUTFRegion, 258 259 GetPrimitiveArrayCritical, 260 ReleasePrimitiveArrayCritical, 261 262 GetStringCritical, 263 ReleaseStringCritical, 264 265 NewWeakGlobalRef, 266 DeleteWeakGlobalRef, 267 268 ExceptionCheck, 269 270 NewDirectByteBuffer, 271 GetDirectBufferAddress, 272 GetDirectBufferCapacity, 273 274 GetObjectRefType 275 };
4.常用函数表
对象(全局和局部)的生成,删除. | |||||||||
jobject NewGlobalRef(JNIEnv *env, jobject obj); |
Creates a new global reference to the object referred to by the The of by calling |
||||||||
void DeleteGlobalRef(JNIEnv *env, jobject globalRef); | Deletes the global reference pointed to by globalRef . |
||||||||
jobject NewLocalRef(JNIEnv *env, jobject ref); |
Creates a new local reference that refers to the same object as local reference. Returns |
||||||||
void DeleteLocalRef(JNIEnv *env, jobject localRef); | Deletes the local reference pointed to by localRef . |
||||||||
类的定义,声明,查找等 | |||||||||
jclass FindClass(JNIEnv *env, const char *name); |
The result of for applications,and is able to locate classes listed in the The For example, the fully-qualified class name for the "java/lang/String"
The array type signature of the array class "[Ljava/lang/Object;" |
||||||||
jclass GetSuperclass(JNIEnv *env, jclass clazz); |
If the superclass of the class specified by this function returns |
||||||||
jclass DefineClass(JNIEnv *env, const char *name, jobject loader,const jbyte *buf, jsize bufLen); |
Loads a class from a buffer of raw class data. The buffer containing the raw class data is not referenced by the VM after the DefineClass call returns, and it may be discarded if desired. |
||||||||
jobject AllocObject(JNIEnv *env, jclass clazz); |
Allocates a new Java object without invoking any of the constructors for the object. Returns a reference to the object. The clazz argument must not refer to an array class. |
||||||||
|
Constructs a new Java object. The method ID indicates which constructor method to invoke. This ID must be obtained by calling ( The |
||||||||
数组相关 | |||||||||
ArrayType NewXXXArray
|
申请数组 A family of operations used to construct a new primitive array object. Table 4-8 describes the specific primitive array constructors.You should replace NewXXXArray with one of the actual primitive array constructor routine names from the following table, and replace ArrayType with the corresponding array type for that routine. |
||||||||
NativeType
返回数组所有元素 |
A family of functions that returns the body of the primitive array. The result is valid until the corresponding Release Java array, changes made to the returned array will not necessarily be reflected in the original
If or it is set to |
||||||||
void ReleaseXXXArrayElements(JNIEnv *env, ArrayType array, NativeType *elems, jint mode); |
A family of functions that informs the VM that the native code no longer needs access to is a pointer derived from copies back all changes made to buffer should be released. following impact, as shown in the following table: Table 4-10 Primitive Array Release Modes
In most cases, programmers pass “0” to the The other options give the programmer more control over memory management and should be used with extreme care. |
||||||||
jsize GetArrayLength(JNIEnv *env, jarray array); | Returns the number of elements in the array. | ||||||||
const NativeType |
A family of functions that copies back a region of a primitive array from a buffer.The following table describes the specific primitive array element accessors. You should make the following replacements: |
||||||||
void GetXXXArrayRegion
|
A family of functions that copies a region of a primitive array into a buffer. | ||||||||
获取对象属性,成员系列 | |||||||||
|
Returns the class of an object. | ||||||||
GetMethodID与GetStaticMethodID系列 jmethodID GetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig); |
Returns the method ID for an instance (nonstatic) method of a class or interface. The method may be defined in one of the
To obtain the method ID of a constructor, supply |
||||||||
GetFieldID与GetFieldID系列 |
Returns the field ID for an instance (nonstatic) field of a class. The field is specified by its name and signature. The Get<type>Field and Set<type>Field families of accessor functions use field IDs to retrieve object fields.
|
||||||||
调用系列 | |||||||||
NativeType CallXXXMethod
|
使用obj,class,等调用值为methoID的函数.注意要用 jobject,不是jclass |
||||||||
NativeType CallStaticXXXMethod
|
This family of operations invokes a static method on a Java object, according to the specified method ID. The The method ID must be derived from |
||||||||
字符串相关 | |||||||||
jstring NewStringUTF(JNIEnv *env, const char *bytes); |
Constructs a new |
||||||||
const char * GetStringUTFChars(JNIEnv *env, jstring string,jboolean *isCopy); |
Returns a pointer to an array of bytes representing the string in modified UTF-8 encoding. This array is valid until it is released by If |
||||||||
环境相关 | |||||||||
jint GetJavaVM(JNIEnv *env, JavaVM **vm); |
Returns the Java VM interface (used in the Invocation API) associated with the current thread. The result is placed at the location pointed to by the second argument, |
||||||||
jboolean ExceptionCheck(JNIEnv *env); |
Returns |
||||||||
等等 | ... |