Android java transmits an array of the int type to C and androidint.
Next we will continue to use the previous article "Android java transfers int type data to C" and Android java transfers string type data to C ".
Implement public native int [] arrElementsIncrease (int [] intArray );
Project Layout
<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 = "passing int type parameters"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "passString" android: text = "passing String type parameters"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "passIntArray" android: text = "passing an array of Int type"/> </LinearLayout>
Implement the arrElementsIncrease Function
/* * 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; }
Add the following code to 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 + ""); } }
Get more C-related knowledge and follow the public account: "csuanfa"