Android JNI Programming 3 (Operations on basic type one-dimensional integer arrays) Summary version

Source: Internet
Author: User

Main learning materials: Black Horse Programmer's Ndk method use (Production class library so)

JNI Programming Guide Chinese (uploaded to blog park)

Blogger article (it uses VS and Eclipse co-development): http://www.cnblogs.com/activity-life/p/3643047.html

//0.incoming one-dimensional integer array with no return value (but modified for array) PublicNativevoidArrayencode (int[] arr);//1.Pass in a one-dimensional array, array length (because C is not easy to get and Java is convenient), return integerPrivateNativeintSumarray (int[] AR,intlength);//Pass the length of an array//2.incoming one-dimensional integer array, infinite set length, return integerPrivateNativeintSumarraya (int[] ar);//No transmission length

Declaring a good local method in Mainactivity.java (implemented by C language)

src folder under javah Instruction compilation generated. h (convenient use of function name)

Configure NDK, (Generate class library, Jni,libs folder)

Modify suffix name:. cpp->.c android.mk source file type modified to. c

C Code:

//0. No return value, enter an array, no lengthJniexportvoidjnicall Java_com_swust_array_mainactivity_arrayencode (jnienv*env, Jobject obj, Jintarray jintarr) {    //get the length of the integer array and the address of the No. 0 element//jsize (*getarraylength) (jnienv*, jarray);        intLength = (*env)getarraylength (env, Jintarr); //jint* (*getintarrayelements) (jnienv*, Jintarray, jboolean*);        int* ARRP = (*env)->getintarrayelements (env, Jintarr, 0); inti;  for(i = 0;i < length; i++){            * (ARRP + i) + = 10; }}//1. There is a return value int type, enter an array, have lengthjniexport jint jnicall java_com_swust_array_mainactivity_sumarray (jnienv*env, Jobject obj, Jintarray arr, jint length) {     intI, sum = 0; Jint* Poutbuf =NULL; if(Length > 0) Poutbuf= (jint*) malloc (length *sizeof (jint)); Else            return0; (*ENV)->getintarrayregion (env, arr, 0, length, poutbuf);  for(i = 0; i < length; i++) Sum+=Poutbuf[i];        Free (POUTBUF); Poutbuf=NULL; returnsum;}//2. There is a return value int type, enter an array, no lengthjniexport jint jnicall Java_com_swust_array_mainactivity_sumarraya (jnienv*env, Jobject obj, Jintarray arr) {    inti,j, sum = 0; Jint*buf; J= (*env)getarraylength (Env,arr); //In addition, the value returned here is jint*, not a const jint*, which means that the values therein can be modifiedBUF = (*env)->getintarrayelements (env, arr, NULL);//This sentence also tells the Java memory collector, do not reclaim the memory of ARR array, or do not defragment the memory//If the collector, recycle and defragment the memory, then we in C, access to the address may be wrong          for(i = 0; i < J; i++)//here is the find -and-fitSum + =Buf[i]; //Now let's change the elements in the BUF and see if we can update the values in the BUF to arr in Java after we return .BUF[0] = 100; buf[1] = 200; (*ENV)->releaseintarrayelements (env, arr, buf, 0);//call this method, tell the Java memory collector that I have run out of arr, and can now reclaim Arr's memory         returnsum;}

Import include (easy to find definition, no warning error)

Calling the local method in Java code

int[] arr = {1,2,3,4,5};  Public voidClick1 (View v) {//Type 1: return value is void type, input one-dimensional array no lengthArrayencode (arr);  for(intI:arr)        {System.out.println (i); }        //Type 2: return value is int, input one-dimensional array has lengthInteger val = Sumarray (arr,5); LOG.D ("WSQ", "after passing array length, and as =" +val); //Type 3: return value int, input one-dimensional array no length//passed in a one-dimensional array, accumulated sum, and modified the value of the No. 0 and 1th array elementsInteger value =Sumarraya (arr); Toast.maketext ( This, value.tostring (), 0). Show (); LOG.D ("WSQ", "arr[0" = "+ arr[0]+" arr[1] "+arr[1]); }    

Effect:

Call Type 1:

Type 2:

Type 3:

Did not learn thoroughly, to apply the main, everybody many advice, 2015-10-25 15:39 notes

Android JNI Programming 3 (Operations on basic type one-dimensional integer arrays) Summary version

Related Article

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.