標籤:
1,關於java端類及介面定義請參考:
android NDK 實用學習-擷取java端類及其類變數
2,對傳過來的參數進行賦值:
env->SetBooleanField(jobj, jfbdata, false);
env->SetDoubleField(jobj, jfddata, 209.22);
env->SetIntField(jobj, jfidata, 3653);
env->SetByteField(jobj, jfcdata, 67);
char data[10] = "4611354"; env->SetObjectField(jobj, jfsdata, env->NewStringUTF(data));
1 // 擷取Java中Int數組賦值 2 jintArray jint_arr = (jintArray)env->GetObjectField(jobj, jiarr); 3 int arrInt[10] = {0}; 4 for(int i = 0; i < 10; ++i){ 5 arrInt[i] = 220+i; 6 } 7 env->SetIntArrayRegion(jint_arr, 0, 10, arrInt); 8 9 // 擷取Java中byte數組賦值10 jfieldID jbyteArr = env->GetFieldID(jcSetDataMgr, "byteArr", "[B");11 char arrByte[10] = "20200825";12 jbyteArray jbyte_arr = (jbyteArray)env->GetObjectField(jobj, jbyteArr); 13 env->SetByteArrayRegion(jbyte_arr, 0, 8, (const signed char*)arrByte);
3,擷取成員變數值:
bool bData = env->GetBooleanField(jcSetDataMgr, jfbdata);
double dData = env->GetDoubleField(jcSetDataMgr, jfddata);
int iData = env->GetIntField(jcSetDataMgr, jfidata);
char cData = env->GetByteField(jcSetDataMgr, jfcdata);
1 jfieldID jfsdata = env->GetFieldID(jcSetDataMgr, "sData", "Ljava/lang/String;");2 jstring jtestStr = (jstring)(env->GetObjectField(jcSetDataMgr, jfsdata));3 const char* testStr = env->GetStringUTFChars(jtestStr, NULL);4 char testData[100] = {0};5 strcpy(testData, testStr);6 env->ReleaseStringUTFChars(jtestStr, testStr);
1 jfieldID jiarr = env->GetFieldID(jcSetDataMgr, "intArr", "[I"); 2 if(NULL == jiarr){ 3 DEBUG_OUT("jfbdata fail!"); 4 } 5 6 // 擷取Java中數組屬性arrays的對象 7 jintArray jint_arr = (jintArray)env->GetObjectField(jobj, jiarr); 8 9 int arrInt[10] = {0};10 for(int i = 0; i < 10; ++i){11 arrInt[i] = 220+i;12 }13 env->GetIntArrayRegion(jint_arr, 0, 10, arrInt);
4,關於類中包含類的可以依次類推,如有用到,以後後續完善!
android NDK 實用學習-java端對象成員賦值和擷取對象成員值