The JNI development of Android

Source: Internet
Author: User

The definition of the variable int i;typedef int x;//defines an int x data type x a=10;printf ("size=%d", sizeof (i));//Gets the int type length (4 bytes) system ("pause");// Do not let the form disappear immediately in the C language, the loop variable must be outside life int ifor (i=0;i<10;i++) {printf ("i=%d", I);} pointer variable int i=10;int* p;//declares a pointer variable p=&i;//take out the address value of I, assign to pointer variable *p take out pointer to value multilevel pointer int i=10;int* p;p=&i;int** s;s=&p;int x;x=&s;//take out the value of I, here is a rule, how many in front of *, get time on the signature plus a few *printf ("i=%d", ***x); array array definition int arr[3] or int arr[]={1,2,3}; Iterate over an array:           1. Ordinary way int i;for (i=0;i<3;i++) {printf ("arr[%d]=%d\n", I,arr[i]); }//2. Get <* (arr+j) >int j;for (j=0;j<3;j++) {printf ("arr[%d]=%d\n", j,* (ARR+J)) by pointer, and define the structure and use//1. Define struct body struct person{int age;} 2. struct person p;p.age=10;printf ("p.age=%d", p.age), struct pointer variable definition and use struct person{int age;} *p;//declares a struct pointer variable// Dynamic Request memory Space p= (struct person*) malloc (sizof (struct person));p->age=18;printf ("p->age=%d", p->age); function definition and use #       Include<stdio.h>int sum (int x,int y);//declares a function main () {int x=10;       int y=10; int z=sum (x, y);//Call function to sum operation printf ("z=%d", z); System ("Pause");} The true function int sum (int x,int y) {return x+y;} The function can also directly define an int sum (int x,int y) {return x+y;}       Main () {int x=10;       int y=10;       int z=sum (x, y);//Call function to sum operation printf ("z=%d", z); System ("Pause");}       Exchange Change2 of two variables (int* x,int* y) {int z=*x;       *x=*y; *y=z;}       Main () {int x=1;       int y=2;       Change2 (&x,&y);       printf ("x=%d\n", X);       printf ("y=%d\n", y); System ("pause"); The pointer function #include<stdio.h>int sum (int x,int y) {return x+y;} main () {int (*s) (int,int);//define a function pointer S=su M       Assign a value int x=s (2,3) to the function pointer;       printf ("x=%d", X); System ("Paud");} Use a function pointer in a struct 1. Define a function 2. A struct, and define a function pointer in the struct 3. In the main function, define a struct pointer 4. Assign values to function pointers through struct pointers, and member assignment #include<stdio.h>//       1. Define a function int sum (int x,int y) {return x+y;}//2. Top a struct, and define a function pointer in the struct struct stu{int age; function pointer int (*score) (int,int);} Main () {//3. Defines a struct pointer struct stu* s= (struct stu*) malloc (sizeof (struct STU*));      4. Assigning a value to a function pointer s->score=sum;            Assigning a value S->age=s->score to a member attribute in a struct;      printf ("age=%d", s->age);        System ("pause");      The definition of union is associated with the use//definition of a union union un{int age; char x;}        Main () {Union UN abc;//defines a union variable abc.age=10;       printf ("age=%d\n", abc.age);       printf ("x=%d\n", abc.x); System ("Pause");} Enumeration using//defines an enumeration in which the members are an integer constant value enum mode{a=100,//if not assigned, the default starting from 0, if the assignment is followed by a second plus 1 B} main () {//define an enum variable enum m       Ode M=a;       printf ("m=%d", m); System ("pause"); } type description typedef int XX, XX a=100;define directive, define global content; In C 1 for true, 0 for false #define TRUE 1; #define FALSE 0;if (True) {...} else{....}   Define a global method # define MAX (x, y) (x>y)? x:y dynamic Array int* arr= (int*) malloc (4*10); file read int main () {file* fp;   Char ch;   if ((Fp=fopen ("Mm.txt", "rt") = = NULL) {printf ("file does not exist or read failed \ n"); }else{//How do I read the contents of a file? CH=FGETC (FP);//Gets the character from the stream while (ch!=eof) {putchar (ch);//output character ch=fgetc (FP) on StdOut;} Fclose (FP);//close a stream} system ("pause");//Pause let cmd window does not disappear}JNI development process: 1 Create a new Android Project 2 Add native support generate JNI folder android.mk file + xx.cpp file 3 Modify file ANDROID.MK. cpp to. C Change the Xx.cpp file to. C----------------------------------------------------------------------------The project can be developed for NDK 4 Add a native method in Mainactivity.java public native String getString (); 5 Generate header file cmd cd/d project src directory javah com.itcast.jn I1. Mainactivity generates a com_itcast_jni1_ in SRC MainActivity.h----------------------------------------------------------------------------generated header file 6 Com_itcast_ Jni1_mainactivity.h Copy to JNI, copy the inside method to. C File 7 Complete the. c File content writing----------------------------------------------------------------------------C code completion 8 compile C code right-click Project-"   Build configuration--buill all generates. So file 9 load dynamic link library file static{system.loadlibrary ("Hello"); }10 calling the native method

 

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.