jni.h jni数据类型 jni常用函数

jni数据类型的定义和jni常用函数的定义可以参考:

 android-ndk-r8b/platforms/android-14/arch-arm/usr/include/jni.h,当然,mips和x86下面也是有的。


1、基本数据类型

#ifdef HAVE_INTTYPES_H
# include <inttypes.h>      /* C99 */
typedef uint8_t         jboolean;       /* unsigned 8 bits */
typedef int8_t          jbyte;          /* signed 8 bits */
typedef uint16_t        jchar;          /* unsigned 16 bits */
typedef int16_t         jshort;         /* signed 16 bits */
typedef int32_t         jint;           /* signed 32 bits */
typedef int64_t         jlong;          /* signed 64 bits */
typedef float           jfloat;         /* 32-bit IEEE 754 */
typedef double          jdouble;        /* 64-bit IEEE 754 */
#else
typedef unsigned char   jboolean;       /* unsigned 8 bits */
typedef signed char     jbyte;          /* signed 8 bits */
typedef unsigned short  jchar;          /* unsigned 16 bits */
typedef short           jshort;         /* signed 16 bits */
typedef int             jint;           /* signed 32 bits */
typedef long long       jlong;          /* signed 64 bits */
typedef float           jfloat;         /* 32-bit IEEE 754 */
typedef double          jdouble;        /* 64-bit IEEE 754 */
#endif


/* "cardinal indices and sizes" */
typedef jint            jsize;



2、数组对象数据类型

从定义上看不出什么

#ifdef __cplusplus
/*
 * Reference types, in C++
 */
class _jobject {};
class _jclass : public _jobject {};
class _jstring : public _jobject {};
class _jarray : public _jobject {};
class _jobjectArray : public _jarray {};
class _jbooleanArray : public _jarray {};
class _jbyteArray : public _jarray {};
class _jcharArray : public _jarray {};
class _jshortArray : public _jarray {};
class _jintArray : public _jarray {};
class _jlongArray : public _jarray {};
class _jfloatArray : public _jarray {};
class _jdoubleArray : public _jarray {};
class _jthrowable : public _jobject {};


typedef _jobject*       jobject;
typedef _jclass*        jclass;
typedef _jstring*       jstring;
typedef _jarray*        jarray;
typedef _jobjectArray*  jobjectArray;
typedef _jbooleanArray* jbooleanArray;
typedef _jbyteArray*    jbyteArray;
typedef _jcharArray*    jcharArray;
typedef _jshortArray*   jshortArray;
typedef _jintArray*     jintArray;
typedef _jlongArray*    jlongArray;
typedef _jfloatArray*   jfloatArray;
typedef _jdoubleArray*  jdoubleArray;
typedef _jthrowable*    jthrowable;
typedef _jobject*       jweak;




#else /* not __cplusplus */


/*
 * Reference types, in C.
 */
typedef void*           jobject;
typedef jobject         jclass;
typedef jobject         jstring;
typedef jobject         jarray;
typedef jarray          jobjectArray;
typedef jarray          jbooleanArray;
typedef jarray          jbyteArray;
typedef jarray          jcharArray;
typedef jarray          jshortArray;
typedef jarray          jintArray;
typedef jarray          jlongArray;
typedef jarray          jfloatArray;
typedef jarray          jdoubleArray;
typedef jobject         jthrowable;
typedef jobject         jweak;


#endif /* not __cplusplus */

3、常用函数

3.1、数组操作函数

NewXXXArray系列函数,jbyteArray NewByteArray(jsize val0);

创建Array对象。val0是Array对象大小。


GetXXXArrayElements系列函数,jbyte *GetByteArrayElements(jbyteArray val0, jboolean *val1);

从Array对象获得数组。val0是Array对象,val1是否是拷贝(直接NULL)。


ReleaseXXXArrayElements系列函数,void ReleaseByteArrayElements(jbyteArray val0, jbyte *val1, jint val2);

说是释放,不知道释放什么了。val0是Array对象,val1是数据,val2如何释放(不知道)。


GetXXXArrayRegion系列函数,void GetByteArrayRegion(jbyteArray val0, jsize val1, jsize val2, jbyte *val3);

SetXXXArrayRegion系列函数,void SetByteArrayRegion(jbyteArray val0, jsize val1, jsize val2, jbyte *val3);

是读取和设置部分Array对象值。


Java_comxxx_xxx(jbyteArray arr){

jbyte *pj, *p;

boolean isCopy = JNI_TRUE;


pj = (*env)->GetByteArrayElements(env, arr, NULL);

LOGI("val0 = %c and val1 = %c and val2 = %c", pj[0], pj[1], pj[2]);


pj[0]='+'; pj[1]='-'; pj[2]='*';

p = (*env)->GetByteArrayElements(env, arr, NULL);

LOGI("val0 = %c and val1 = %c and val2 = %c", p[0], p[1], p[2]);


(*env)->ReleaseByteArrayElements(env, arr, pj, JNI_ABORT);

pj[0]='1'; pj[1]='2'; pj[2]='3';

p = (*env)->GetByteArrayElements(env, arr, NULL);

LOGI("val0 = %c and val1 = %c and val2 = %c", p[0], p[1], p[2]);

}

打印结果如下:

val0 = A and val1 = B and val2 = C

val0 = + and val1 = - and val2 = *

val0 = 1 and val1 = 2 and val2 = 3

可以看得出GetByteArrayElements后,pj的改变就会影响arr的值,本以为ReleaseByteArrayElements后就不会影响了,结果还是影响,不知道是不是我使用方法不对。


3.2 String操作

char *GetStringUTFChars(jstring string, jboolean *isCopy);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值