接着前面的文章《Android java传递int类型数据给C》《Android java传递string类型数据给C》,继续实践
实现public native int[] arrElementsIncrease(int[] intArray);
项目布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="传递int类型参数" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="passString" android:text="传递String类型参数" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="passIntArray" android:text="传递Int类型数组" /> </LinearLayout>
实现arrElementsIncrease函数
/* * Class: com_wuyudong_javapassdata_JNI * Method: arrElementsIncrease * Signature: ([I)[I */JNIEXPORT jintArray JNICALL Java_com_wuyudong_javapassdata_JNI_arrElementsIncrease( JNIEnv * env, jobject clazz, jintArray jArray){ jsize len = (*env)->GetArrayLength(env, jArray); // jboolean iscopy; int* arrayPointer = (*env)->GetIntArrayElements(env, jArray, NULL); int i; for(i = 0; i < len; i++){ *(arrayPointer + i) += 10; } return jArray; }
MainActivity.java中添加下面的代码:
public void passIntArray(View v) { int[] array = new int[] { 1, 2, 3, 4, 5 }; int[] newArray = jni.arrElementsIncrease(array); for (int i : newArray) { Log.d("test", i + ""); } }