1. Declaration Native method
public class Complexobject {/** * Returns an array of objects * @param val * @return */public native person[] Receiverbeanarray (String val); * * Get Object * @return */public Native person Getperson ();/** * Get collection of objects * @return */public native arraylist<person> get Personlist ();}
2. Person Class objectThere are two constructors, primarily to create a person object from a different constructor in JNI.
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.) {this.age = age;} @Overridepublic String toString () {return ' person [name= + name + ', age= ' + Age + '] ";}}
3. Implementation of JNI operationin JNI, constructors can be invoked just like instance methods, and are similarly called. Pass in "<init>" as the method name, "V" as the return type. You can call the constructor by passing the NewObject method to the JNI function.
/** * Stitching string */char* strjoin (char *s1, char *s2) {char *result = malloc (strlen (S1) + strlen (s2) + 1); if (result = = NULL) exi T (1); strcpy (result, S1); strcat (result, S2); return result;} /** * Returns an array type to Java */jniexport jobjectarray jnicall java_com_example_jniandroid_service_complexobject_ Receiverbeanarray (JNIENV * env, Jobject obj, jstring string) {Jclass objclass = (*env)->findclass (env, "java/lang/ob Ject "); 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;//convert jstring type to char*char* nameStr = (char*) (*env)->getstringutfchars (env,string, 0);//splicing string const char* name = Strjoin (nameStr, "-join jni"); LOGD ("Array sum =%s", name), for (; i < array_length; i++) {int = 20+i;//add Java string and C string to the name attribute (*env)->setobje Ctfield (env, Mperson, Field_name, (*env)->newstringutf (env,name));(*env)->setintfield (Env,mperson, Field_age , age);(*env)->setobjectarrayelement (env, mails, (jsize) I, Mperson);} return mails;} /** * Set values for Java objects in JNI and return */jniexport jobject jnicall Java_com_example_jniandroid_service_complexobject_getperson ( JNIENV * env, Jclass obj) {//Find person class Jclass M_cls = (*env)->findclass (env, "Com/example/jniandroid/bean/person");// The parameterless construction method Jmethodid M_mid = (*env)->getmethodid (env,m_cls, "<init>", "() V");//Gets the complex type in Java starting with L; The end, the middle is the type will. Change to/ Jfieldid m_fid_name = (*env)->getfieldid (env,m_cls, "name", "ljava/lang/string;"); /base type, I means intjfieldid m_fid_age = (*env)->getfieldid in Java (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;} /** * Returns ArrayList collection */jniexport Jobject jnicall java_com_example_jniandroid_service_complexobject_getpersonlist ( JNIENV * env, Jobject obj) {jclass list_cls = (*env)->findclass (env, "java/util/arraylist");// Get ArrayList class reference Jmethodid list_costruct = (*env)->getmethodid (env, LIST_CLS, "<init>", "() V"); Obtained constructor idjobject list_obj = (*env)->newobject (env, LIST_CLS, list_costruct); Create an Add () method ID in the ArrayList collection object//arraylist class with the method prototype: Boolean Add (Object object), and the Boolean type corresponds to Z;jmethodid list_add = (* ENV)->getmethodid (env, LIST_CLS, "Add", "(Ljava/lang/object;) Z"); Jclass Personclass = (*env)->findclass (env, " Com/example/jniandroid/bean/person "); Get the Person class reference//Get the constructor function of the type named <init> return type must be void i.e. Vjmethodid person_costRuct = (*env)->getmethodid (env, Personclass, "<init>", "(iljava/lang/string;) V"); int i=0;for (; i < 5; i++) {J String str = (*env)->newstringutf (env, "JNI Native");//By calling the object's constructor to new a student instance jobject person_obj = (*env) NewObject (env, personclass,person_costruct, I, str); Constructs an object//executes the Add method of the ArrayList class instance, adds a person object (*env)->callbooleanmethod (Env,list_obj, List_add, person_obj);} return list_obj;}
The application of _6_ Simple object in JNI easy-to-digest