Android-NDK开发之第三个例子--传递字符串数组和中文乱码问题

来源:互联网 发布:淘宝店铺在线装修 编辑:程序博客网 时间:2024/05/18 00:02

转自:http://blog.csdn.net/geolo/article/details/5954272


声明:部分内容出自网络。

     这里说明下Android中的JNI的中文乱码问题。   我们新建一个native.c的时候。eclipse对native.c默认的是GBK。我试着在java中把GBK转为UTF-8,依旧乱码,因此我也不知道jni返回的中文字符串是什么类型。

     经过不断猜测,就是我们在新建了native.c的时候,就把native.c的编码类型改为UTF-8,这样在java中也不要转编译了,直接就可以在模拟器上显示出中文。

     步骤:新建native.c,在native.c文件上右击鼠标-->properties-->Text file encoding下的选项改为UTF-8即可。

 

AndroidNDKSample.java

    package com.geolo.android;      import android.app.Activity;      import android.os.Bundle;      import android.widget.TextView;      public class AndroidNDKSample extends Activity {          /** Called when the activity is first created. */          @Override          public void onCreate(Bundle savedInstanceState) {              super.onCreate(savedInstanceState);              setContentView(R.layout.main);              TextView testNDK = (TextView)findViewById(R.id.test);              String strArrstr ="";              String strArr[] = getStringArray("to C string: ");              for(String s : strArr){                  strArrstr += ("/n" + "my: " + s);              }              testNDK.setText(strArrstr);          }          static{              System.loadLibrary("native");          }          public native String[] getStringArray(String string);      }  

 

main.xml

    <?xml version="1.0" encoding="utf-8"?>      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"          android:orientation="vertical"          android:layout_width="fill_parent"          android:layout_height="fill_parent"          >      <TextView            android:id="@+id/test"          android:layout_width="fill_parent"           android:layout_height="wrap_content"           android:text="@string/hello"          />      </LinearLayout>  

 

Android.mk

    LOCAL_PATH := $(call my-dir)      include $(CLEAR_VARS)      LOCAL_MODULE    := native      LOCAL_SRC_FILES := myNative.c      include $(BUILD_SHARED_LIBRARY)  

 

native.c    (要改为UTF-8 格式)

    #include <stdio.h>      #include <stdlib.h>      #include <string.h>      #include <jni.h>      #define ARRAY_LENGTH 5      jobjectArray Java_com_geolo_android_AndroidNDKSample_getStringArray(JNIEnv *env ,      jobject obj , jstring string){          jclass objClass = (*env)->FindClass(env , "java/lang/String");          jobjectArray texts = (*env)->NewObjectArray(env ,                                       (jsize)ARRAY_LENGTH , objClass , 0);          jstring jstr;          char* sa[] = {"HelloNDK!!" , "Geolo" , "JNI" , "你好" , "我不好"};          int i = 0;          for(; i<ARRAY_LENGTH ; i++){             jstr = (*env)->NewStringUTF(env , sa[i]);             (*env)->SetObjectArrayElement(env, texts, i, jstr);//必须放入jstring          }          return texts;      }  

 

      本节例程是对上节例程的进一步深化:虽然仍然是传递数组,但是数组的基类换成了字符串这样一种对象数据类型。Java程序将向C程序传入一个包含中文字符的字符串,C程序并没有处理这个字符串,而是开辟出一个新的字符串数组返回给Java程序,其中还包含两个汉字字符串。

      JNI框架并没有定义专门的字符串数组,而是使用jobjectArray——对象数组,对象数组的基类是jclass,jclass是JNI框架内特有的类型,相当于Java语言中的Class类型。在本例程中,通过FindClass()函数在JNI上下文中获取到java.lang.String的类型 (Class),并将其赋予jclass变量。

      在例程中我们定义了一个长度为5的对象数组texts,并在程序中向其中循环放入预先定义好的sa数组中的字符串,当然前置条件是使用NewStringUTF()函数将C语言的字符串转换为jstring类型。

原创粉丝点击