Android java transmits an array of the int type to C and androidint.

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.