主要操做內容,包括以下幾個部分:java
一、在Native層返回一個字符串數組
二、從Native層返回一個int型二維數組(int a[ ][ ]) 函數
三、從Native層操做Java層的類: 讀取/設置類屬性this
四、在Native層操做Java層的類:讀取/設置類屬性、回調Java方法 spa
五、從Native層返回一個複雜對象(即一個類咯).net
六、在Java層傳遞複雜對象至Native層對象
七、從Native層返回Arraylist集合對象blog
廣而告知,這些操做就是簡單的利用一些JNI函數即實現了。so easy 。ip
1、在Native層返回一個字符串
Java層原型方法:字符串
- public class HelloJni {
- ...
- public native void getAJNIString();
- ...
- }
Native層該方法實現爲 :
- JNIEXPORT jstring JNICALL Java_com_feixun_jni_HelloJni_getAJNIString(JNIEnv * env, jobject obj)
- {
- jstring str = env->newStringUTF("HelloJNI");
- return str ;
- }
2、在Native層返回一個int型二維數組(inta[ ][ ])
Java層原型方法:
- public class HelloJni {
- ...
-
- private native int[][] getTwoArray(int dimon) ;
- ...
- }
Native層該方法實現爲 :
- JNIEXPORT jobjectArray JNICALL Java_com_feixun_jni_HelloJni_getTwoArray
- (JNIEnv * env, jobject object, jint dimion)
- {
-
- jclass intArrayClass = env->FindClass("[I");
-
- jobjectArray obejctIntArray = env->NewObjectArray(dimion ,intArrayClass , NULL);
-
-
- for( int i = 0 ; i< dimion ; i++ )
- {
-
- jintArray intArray = env->NewIntArray(dimion);
-
- jint temp[10] ;
- for( int j = 0 ; j < dimion ; j++)
- {
- temp[j] = i + j ;
- }
-
-
- env->SetIntArrayRegion(intArray, 0 , dimion ,temp);
-
- env->SetObjectArrayElement(obejctIntArray , i ,intArray);
-
- env->DeleteLocalRef(intArray);
- }
-
- return obejctIntArray;
- }
3、在Native層操做Java層的類 :讀取/設置類屬性
Java層原型方法:
- public class HelloJni {
- ...
-
- public native void native_set_name() ;
- ...
-
- private String name = "I am at Java" ;
- }
Native層該方法實現爲 :
- JNIEXPORT void JNICALL Java_com_feixun_jni_HelloJni_native_1set_1name
- (JNIEnv *env , jobject obj )
- {
-
- jfieldID nameFieldId ;
-
- jclass cls = env->GetObjectClass(obj);
-
- nameFieldId = env->GetFieldID(cls , "name" , "Ljava/lang/String;");
-
- if(nameFieldId == NULL)
- {
- cout << " 沒有獲得name 的句柄Id \n;" ;
- }
- jstring javaNameStr = (jstring)env->GetObjectField(obj ,nameFieldId);
- const char * c_javaName = env->GetStringUTFChars(javaNameStr , NULL);
- string str_name = c_javaName ;
- cout << "the name from java is " << str_name << endl ;
- env->ReleaseStringUTFChars(javaNameStr , c_javaName);
-
-
- char * c_ptr_name = "I come from Native" ;
-
- jstring cName = env->NewStringUTF(c_ptr_name);
-
- env->SetObjectField(obj , nameFieldId , cName);
- }
4、在Native層操做Java層的類:回調Java方法
Java層原型方法:
- public class HelloJni {
- ...
-
- public void callback(String fromNative){
- System.out.println(" I was invoked by native method ############# " + fromNative);
- };
- public native void doCallBack();
- ...
-
-
- public static void main(String[] args)
- {
- new HelloJni().ddoCallBack();
- }
- }
Native層該方法實現爲 :
- JNIEXPORT void JNICALL Java_com_feixun_jni_HelloJni_doCallBack
- (JNIEnv * env , jobject obj)
- {
-
-
- jclass cls = env->GetObjectClass(obj);
- jmethodID callbackID = env->GetMethodID(cls , "callback" , "(Ljava/lang/String;)V") ;
-
- if(callbackID == NULL)
- {
- cout << "getMethodId is failed \n" << endl ;
- }
-
- jstring native_desc = env->NewStringUTF(" I am Native");
-
- env->CallVoidMethod(obj , callbackID , native_desc);
- }
接下來,咱們會操做複雜對象,也就是Java層的類,包括從Native層返回一個類以及傳遞一個類到Native層去, 這兒咱們
使用的類很是簡單,以下:
Student.java類
- package com.feixun.jni;
-
- public class Student
- {
- private int age ;
- private String name ;
-
- public Student(){ }
-
- public Student(int age ,String name){
- this.age = age ;
- this.name = name ;
- }
-
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name){
- this.name = name;
- }
-
- public String toString(){
- return "name --- >" + name + " age --->" + age ;
- }
- }
5、在Native層返回一個複雜對象(即一個類咯)
Java層的方法對應爲:
- public class HelloJni {
- ...
-
- public native Student nativeGetStudentInfo() ;
- ...
- }
Native層該方法實現爲 :
- JNIEXPORT jobject JNICALL Java_com_feixun_jni_HelloJni_nativeGetStudentInfo
- (JNIEnv * env, jobject obl)
- {
-
-
- jclass stucls = env->FindClass("com/feixun/jni/Student");
-
-
- jmethodID constrocMID = env->GetMethodID(stucls,"<init>","(ILjava/lang/String;)V");
-
- jstring str = env->NewStringUTF(" come from Native ");
-
- jobject stu_ojb = env->NewObject(stucls,constrocMID,11,str);
-
-
- return stu_ojb ;
- }
6、從Java層傳遞複雜對象至Native層
Java層的方法對應爲:
- public class HelloJni {
- ...
-
- public native void printStuInfoAtNative(Student stu);
- ...
- }
Native層該方法實現爲 :
- JNIEXPORT void JNICALL Java_com_feixun_jni_HelloJni_printStuInfoAtNative
- (JNIEnv * env, jobject obj, jobject obj_stu)
- {
-
- jclass stu_cls = env->GetObjectClass(obj_stu);
-
- if(stu_cls == NULL)
- {
- cout << "GetObjectClass failed \n" ;
- }
-
- jfieldID ageFieldID = env->GetFieldID(stucls,"age","I");
- jfieldID nameFieldID = env->GetFieldID(stucls,"name","Ljava/lang/String;");
-
- jint age = env->GetIntField(objstu , ageFieldID);
- jstring name = (jstring)env->GetObjectField(objstu , nameFieldID);
-
- const char * c_name = env->GetStringUTFChars(name ,NULL);
-
- string str_name = c_name ;
- env->ReleaseStringUTFChars(name,c_name);
-
- cout << " at Native age is :" << age << " # name is " << str_name << endl ;
- }
7、最後加個難度,即在Native層返回集合對象(留這兒,之後也好找點)
Java層的對應方法爲:
- public class HelloJni {
- ...
-
- public native ArrayList<Student> native_getListStudents();
- ...
- }
Native層該方法實現爲 :
- JNIEXPORT jobject JNICALL Java_com_feixun_jni_HelloJni_native_getListStudents
- (JNIEnv * env, jobject obj)
- {
- jclass list_cls = env->FindClass("Ljava/util/ArrayList;");
-
- if(listcls == NULL)
- {
- cout << "listcls is null \n" ;
- }
- jmethodID list_costruct = env->GetMethodID(list_cls , "<init>","()V");
-
- jobject list_obj = env->NewObject(list_cls , list_costruct);
-
- jmethodID list_add = env->GetMethodID(list_cls,"add","(Ljava/lang/Object;)Z");
-
- jclass stu_cls = env->FindClass("Lcom/feixun/jni/Student;");
-
- jmethodID stu_costruct = env->GetMethodID(stu_cls , "<init>", "(ILjava/lang/String;)V");
-
- for(int i = 0 ; i < 3 ; i++)
- {
- jstring str = env->NewStringUTF("Native");
-
- jobject stu_obj = env->NewObject(stucls , stu_costruct , 10,str);
-
- env->CallBooleanMethod(list_obj , list_add , stu_obj);
- }
-
- return list_obj ;
- }