JNI由淺入深_6_簡單對象的應用

一、聲明native方法

public class ComplexObject {

	/**
	 * 返回一個對象數組
	 * @param val
	 * @return
	 */
	public native Person[] receiverBeanArray(String val);

	/**
	 * 獲取對象
	 * @return
	 */
	public native Person getPerson();

	/**
	 * 獲取對象的集合
	 * @return
	 */
	public native ArrayList<Person> getPersonList();

}

二、Person類對象

有兩個構造函數,主要是爲了在jni中經過不一樣的構造函數來建立Person對象。

public class Person {

	public String name;
	private int age;

	public Person() {
		super();
	}

	public Person(int age, String name) {
		super();
		this.age = age;
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}

}

三、jni操做實現

JNI中,構造函數能夠和實例方法同樣被調用,調用方式也類似。傳入「<init>」做爲方法名,「V」做爲返回類型。你能夠經過向JNI函數NewObject傳入方法來調用構造函數。

/**
 * 拼接字符串
 */
char* strJoin(char *s1, char *s2) {
	char *result = malloc(strlen(s1) + strlen(s2) + 1);
	if (result == NULL)
		exit(1);
	strcpy(result, s1);
	strcat(result, s2);
	return result;
}
/**
 * 返回一個數組類型到Java
 */
JNIEXPORT jobjectArray JNICALL Java_com_example_jniandroid_service_ComplexObject_receiverBeanArray
  (JNIEnv * env, jobject obj , jstring string) {
	jclass objClass = (*env)->FindClass(env, "java/lang/Object");
	jobjectArray mails = (*env)->NewObjectArray(env, (jsize) ARRAY_LENGTH,objClass, 0);
	jclass personClass = (*env)->FindClass(env,"com/example/jniandroid/bean/Person");

	jfieldID field_name = (*env)->GetFieldID(env, personClass, "name","Ljava/lang/String;");
	jmethodID construction_id = (*env)->GetMethodID(env, personClass, "<init>","()V");
	jfieldID field_age = (*env)->GetFieldID(env,personClass, "age", "I");
	jclass mPerson = (*env)->NewObject(env, personClass, construction_id);

	int i = 0;
	//將jstring類型轉成char*
	char* nameStr = (char*)(*env)->GetStringUTFChars(env,string, 0);
	//拼接字符串
	const char* name = strJoin(nameStr,"-join jni");
	LOGD("數組總和 =%s", name);
	for (; i < ARRAY_LENGTH; i++) {
		int age = 20+i;
		//將Java的字符串和c的字符串進行拼接後賦值給name屬性
		(*env)->SetObjectField(env, mPerson, field_name, (*env)->NewStringUTF(env,name));
		(*env)->SetIntField(env,mPerson, field_age,age);
		(*env)->SetObjectArrayElement(env, mails, (jsize)i, mPerson);
	}
	return mails;
}


/**
 * 在jni中給Java對象設置值並返回
 */
JNIEXPORT jobject JNICALL Java_com_example_jniandroid_service_ComplexObject_getPerson
  (JNIEnv * env, jclass obj) {
	//找到Person類
	jclass m_cls = (*env)->FindClass(env,"com/example/jniandroid/bean/Person");
	//無參數的構造方法
	jmethodID m_mid = (*env)->GetMethodID(env,m_cls, "<init>", "()V");
	//獲取Java中複雜類型以L開始;結尾,中間是類型將.改爲/
	jfieldID m_fid_name = (*env)->GetFieldID(env,m_cls, "name", "Ljava/lang/String;");
	//基本類型,I表示Java中的int
	jfieldID m_fid_age = (*env)->GetFieldID(env,m_cls, "age", "I");

	if(m_fid_name==NULL){
		printf("m_fid_name is null ");
		return;
	}
	if(m_fid_age==NULL){
		printf("m_fid_age is null ");
		return;
	}

	jobject m_obj = (*env)->NewObject(env,m_cls, m_mid);

	(*env)->SetObjectField(env,m_obj, m_fid_name, (*env)->NewStringUTF(env,"Set the name Person object in the JNI value"));
	(*env)->SetIntField(env,m_obj, m_fid_age,96);

	return m_obj;
}

/**
 * 返回ArrayList集合
 */
JNIEXPORT jobject JNICALL Java_com_example_jniandroid_service_ComplexObject_getPersonList(
		JNIEnv * env, jobject obj) {
	jclass list_cls = (*env)->FindClass(env, "java/util/ArrayList"); //得到ArrayList類引用

	jmethodID list_costruct = (*env)->GetMethodID(env, list_cls, "<init>","()V"); //得到得構造函數Id
	jobject list_obj = (*env)->NewObject(env, list_cls, list_costruct); //建立一個Arraylist集合對象
	//Arraylist類中的 add()方法ID,其方法原型爲: boolean add(Object object) ,boolean類型對應的是Z;
	jmethodID list_add = (*env)->GetMethodID(env, list_cls, "add","(Ljava/lang/Object;)Z");

	jclass personClass = (*env)->FindClass(env,"com/example/jniandroid/bean/Person"); //得到Person類引用
	//得到該類型的構造函數  函數名爲 <init> 返回類型必須爲 void 即 V
	jmethodID person_costruct = (*env)->GetMethodID(env, personClass, "<init>","(ILjava/lang/String;)V");
	int i=0;
	for (; i < 5; i++) {
		jstring str = (*env)->NewStringUTF(env, "jni Native");
		//經過調用該對象的構造函數來new 一個 Student實例
		jobject person_obj = (*env)->NewObject(env, personClass,person_costruct, i, str); //構造一個對象
		//執行Arraylist類實例的add方法,添加一個Person對象
		(*env)->CallBooleanMethod(env,list_obj, list_add, person_obj);
	}
	return list_obj;
}
相關文章
相關標籤/搜索