JNI programming tips (continuous update)

Source: Internet
Author: User

1. The string passed in by Java is converted to char * in C and printed

Jstring OBJ;

Const char * string = (char *) (* env)-> getstringutfchars (ENV, OBJ, null );

_ Android_log_print (android_log_info, "jnimsg", string );

2. The 'for' loop initial declarations are only allowed in c99 mode Error

Int I = 0;

For (; I <size; I ++ ){}

Int I needs to be defined before

3. Obtain the Elements marked as I in the array.

Jobject OBJ = (* env)-> getobjectarrayelement (ENV, array, I ));

4. JNI defines the string array and assigns a value to the string array.

Jobjectarray arrays = 0;
// Define the length
Jsize Len = 20;
Arrays = (* jnienv)-> newobjectarray (jnienv, Len, (* jnienv)-> findclass (
Jnienv, "Java/lang/string"), 0 );

Jstring jstr;
Char * Sa [] = {"hello,", "World! "," JNI "," very "," fun "};
Int I = 0;
For (; I <Len; I ++)
{
Jstr = (* env)-> newstringutf (ENV, sa [I]);
(* Env)-> setobjectarrayelement (ENV, arrays, I, jstr); // jstring must be included
}

5. Obtain the array value

Int K = 0;
For (; k <Len; k ++ ){
Jstring string = (jstring) (* jnienv)-> getobjectarrayelement (jnienv,
Arrays, k ));
If (string! = NULL ){
Char * c = (* jnienv)-> getstringutfchars (jnienv, String, 0 );
_ Android_log_print (android_log_info, "jnimsg", C );
}
}

6. intermediate layer data type conversion

char * ---> jstring (* jnienv)-> newstringutf (jnienv, char *)
jstring ---> char (* jnienv)-> getstringutfchars (jnienv, jstring, 0);
long ---> char * ltoa (long, char *, 10); sprintf (csendbodylen, "% lD", sendbodylen );
char * ---> long * long = atol (char *);

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.