Statement: some content is from the network.
The following describes the Chinese garbled characters of JNI in Android. When we create a new Native. C. Eclipse defaults to GBK for native. C. I tried to convert GBK into a UTF-8 in Java, still garbled, so I don't know what type of Chinese string JNI returned.
After continuous speculation, we have created native. c, the native. c encoding type changed to UTF-8, so that in Java also do not translate, directly on the simulator can display Chinese.
Step: Create native. C, right-click the native. c file and choose Properties> text file encoding> UTF-8.
Androidndksample. Java
Package COM. geolo. android; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; <br/> public class androidndksample extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> textview testndk = (textview) findviewbyid (R. id. test); <br/> string strarrstr = ""; <br/> string strarr [] = getstringarray ("to C string :"); <br/> for (string S: strarr) {<br/> strarrstr + = ("/N" + "My:" + S ); <br/>}< br/> testndk. settext (strarrstr); <br/>}< br/> static {<br/> system. loadlibrary ("native"); <br/>}< br/> Public native string [] getstringarray (string); <br/>}
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: id = "@ + ID/test" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Hello" <br/> </linearlayout> <br/>
Android. mk
Local_path: = $ (call my-DIR) <br/> include $ (clear_vars) <br/> local_module: = native <br/> local_src_files: = mynative. c <br/> include $ (build_shared_library)
Native. C (to be changed to UTF-8 format)
# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> # include <JNI. h> <br/> # define array_length 5 <br/> jobjectarray java_com_geolo_android_androidndksample_getstringarray (jnienv * ENV, <br/> jobject OBJ, jstring string) {<br/> jclass objclass = (* env)-> findclass (ENV, "Java/lang/string"); <br/> jobjectarray texts = (* env) -> newobjectarray (ENV, <br/> (jsize) array_length, objcia SS, 0); <br/> jstring jstr; <br/> char * Sa [] = {"hellondk !! "," Geolo "," JNI "," hello "," I am not good "};< br/> int I = 0; <br/> for (; I <array_length; I ++) {<br/> jstr = (* env)-> newstringutf (ENV, sa [I]); <br/> (* env) -> setobjectarrayelement (ENV, texts, I, jstr); // jstring must be included <br/>}< br/> return texts; <br/>}< br/>
This routine is further deepened in the previous routine: although it is stillPassing ArraysBut the base class of the array is replaced with a string object data type. The Java program will pass in a string containing Chinese characters to the C program. The C program does not process this string, but opens up a new string array and returns it to the Java program, it also contains two Chinese character strings.
JNIThe framework does not define a special string array, but uses jobjectarray -- Object array. The base class of the object array is jclass, and jclass isJNIThe Framework's unique type is equivalent to the class type in Java. In this example, the findclass () functionJNIObtain the java. Lang. string type in the context.
(Class), and assign it to the jclass variable.
In the routine, we define an array of objects with a length of 5, texts, and put the pre-defined strings in the SA array into the loop in the program. Of course, the precondition is to use newstringutf () the function converts a C string to the jstring type.