Android JNI Programming 1 (Operation on a basic type string)

Source: Internet
Author: User

Recently, I have been learning about the JNI of Android, the main reference is the Black Horse Programmer's video tutorial, really good.

Then summarize it yourself, as a review of the study.

This blog is also the main reference to the blogger: http://www.cnblogs.com/activity-life/p/3643047.html, here thank you

Through his blog and video tutorials, I combined the two methods, the function realized, after all, this is the application of the main, their own not fine, here accumulate under:

Summary of the first blog all Java fetch JNI is about string operations, including the following three types:

//access to a basic data type string//0. No input, with output as String    Public nativeString HELLOFROMC (); //1. Have input, have output access string   Private nativestring getnativestring (String str); //2. Input string, with length, output string   Public nativestring encode (string text,intlength);  Public nativeString decode (string text,intlength);

So, according to the tutorial, when an input string is passed as a parameter, it is converted to a C-language string function pointer using the following function:

//string that converts a Java string into CChar* JSTRING2CSTR (jnienv*env, jstring jstr) {     Char* Rtn =NULL; Jclass clsstring= (*env)->findclass (env, "java/lang/string")); Jstring Strencode= (*env)->newstringutf (env, "GB2312")); Jmethodid Mid= (*env)->getmethodid (env,clsstring, "GetBytes", "(ljava/lang/string;) [B]); Jbytearray Barr= (Jbytearray) (*env)->callobjectmethod (Env,jstr,mid,strencode);//String. GetByte ("GB2312");Jsize alen = (*env)getarraylength (Env,barr); Jbyte* ba = (*env)getbytearrayelements (Env,barr,jni_false); if(Alen > 0) {Rtn=   (Char*) malloc (alen+1);//" the"memcpy (Rtn,ba,alen); Rtn[alen]=0; }     (*env)->releasebytearrayelements (env,barr,ba,0);//     returnRtn;}

After that, I'll show the use of JNI in the above three types, respectively:

0. No input, there is output as a string:
Jstring JAVA_COM_SWUST_STRING_MAINACTIVITY_HELLOFROMC (jnienv* env,jobject obj) {   //  C language String    Char* cstr= "Hello Paopao from C";    // converting a C string to a Java string     // jstring     (*NEWSTRINGUTF) (jnienv*, const char*);     // jstring jstr= (* (*env)). Newstringutf (ENV,CSTR)    jstring jstr = (*env), Newstringutf (env, CStr);     return jstr;}
1. There is an input, there is an output access string:
//string that converts a Java string into CChar* JSTRING2CSTR (jnienv*env, jstring jstr) {     Char* Rtn =NULL; Jclass clsstring= (*env)->findclass (env, "java/lang/string")); Jstring Strencode= (*env)->newstringutf (env, "GB2312")); Jmethodid Mid= (*env)->getmethodid (env,clsstring, "GetBytes", "(ljava/lang/string;) [B]); Jbytearray Barr= (Jbytearray) (*env)->callobjectmethod (Env,jstr,mid,strencode);//String. GetByte ("GB2312");Jsize alen = (*env)getarraylength (Env,barr); Jbyte* ba = (*env)getbytearrayelements (Env,barr,jni_false); if(Alen > 0) {Rtn=   (Char*) malloc (alen+1);//" the"memcpy (Rtn,ba,alen); Rtn[alen]=0; }     (*env)->releasebytearrayelements (env,barr,ba,0);//     returnRtn;} Jniexport jstring jnicall java_com_swust_string_mainactivity_getnativestring (jnienv*env, Jobject obj, jstring jstr) {    //gets the input string    Char* cstr=jstring2cstr (ENV,JSTR); Char* Putf8str = "from Jni String"; strcat (CStr,"/");//Add a delimiterstrcat (CStr, PUTF8STR);//joins a local string//C-language string conversion to jstring    return(*env)Newstringutf (ENV,CSTR);}

The basic function of your own implementation is to merge and return the string of Java passed in and C.

2. Input string, with length, output string:
This type is based on the tutorial two functions (encoding and decoding), which can be considered the same type:
//string that converts a Java string into CChar* JSTRING2CSTR (jnienv*env, jstring jstr) {     Char* Rtn =NULL; Jclass clsstring= (*env)->findclass (env, "java/lang/string")); Jstring Strencode= (*env)->newstringutf (env, "GB2312")); Jmethodid Mid= (*env)->getmethodid (env,clsstring, "GetBytes", "(ljava/lang/string;) [B]); Jbytearray Barr= (Jbytearray) (*env)->callobjectmethod (Env,jstr,mid,strencode);//String. GetByte ("GB2312");Jsize alen = (*env)getarraylength (Env,barr); Jbyte* ba = (*env)getbytearrayelements (Env,barr,jni_false); if(Alen > 0) {Rtn=   (Char*) malloc (alen+1);//" the"memcpy (Rtn,ba,alen); Rtn[alen]=0; }     (*env)->releasebytearrayelements (env,barr,ba,0);//     returnRtn;} Jniexport jstring jnicall java_com_swust_string_mainactivity_getnativestring (jnienv*env, Jobject obj, jstring jstr) {    //gets the input string    Char* cstr=jstring2cstr (ENV,JSTR); Char* Putf8str = "from Jni String"; strcat (CStr,"/");//Add a delimiterstrcat (CStr, PUTF8STR);//joins a local string//C-language string conversion to jstring    return(*env)Newstringutf (ENV,CSTR);} Jniexport jstring jnicall java_com_swust_string_mainactivity_encode (jnienv*env, Jobject obj, jstring jstr, jint length) {  //generates a C-language character pointer based on the function given by the teacher//Arg1:java Environment pointer Arg2:java string    Char* cstr=jstring2cstr (ENV,JSTR); intI=0; //The C language is the same as int and jint, so use length directly     for(i=0;i<length;i++){        * (cstr+i) +=1; }    //C-language string conversion to jstring    return(*env)Newstringutf (ENV,CSTR);}

For the code to look clear, the project code is not posted here, only show:

0. No input, with output as String: Display "Hello Paopao from C"
1. Have input, have output access string: Display "Hello from Java/hello from JNI"
2. Input string, with length, output string: Display encoding and decoding capabilities:

Android JNI Programming 1 (Operation on a basic type string)

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.