Android's JNI "hands-on tutorials" 4⃣️--c invoke Java code __java

Source: Internet
Author: User
Basic Process

non-static methods

Get byte code (FINDCLASS)
Get Method (Getmethodid)
Instantiation of the Class (Allocobject)
Call Method (Callintmethod)

static Method

Get byte code (FINDCLASS)
Get Method (Getstaticmethodid)
static method of calling method (Callstaticvoidmethod) non-static method C code addint (int a, int b) method in Java class

Java code:

    public int AddInt (int a, int b) {
        int c = + b;
        Toast.maketext (Mainapplication.getcontext (), "calculated result::" +c, Toast.length_short). Show ();
        return a+b;
    }

. h Code:

jniexport void Jnicall
java_com_libin_factory_ndk_ndk_ccallbackaddint
   (jnienv *env, jclass type);

C + + code:

/**
 * C code modulation Java class AddInt (int a, int b) method
 * *
jniexport void Jnicall
Java_com_libin_factory_ndk_ndk_ Ccallbackaddint (jnienv *env, Jclass type) {
    //Get byte code
    jclass jclazz = Env->findclass ("com/libin/factory/ndk/ Test "); Com.libin.factory.ndk.TestK point to Slip line
    //Get method
    Jmethodid jmethodids = Env->getmethodid (Jclazz, "AddInt", "( II) I ");
    Instantiate the class of
    jobject jobj = Env->allocobject (jclazz);
    Call method
    Jint value =env->callintmethod (jobj,jmethodids,99,1);

    LOGE ("value =%d\n", value);
}

Call:

Ndk.ccallbackaddint ();

Show:
C code-addint (int a, int b) method in Java class

Java code:

    public void Setstrings (String s) {
        toast.maketext (Mainapplication.getcontext (), S, Toast.length_short). Show ();
    

. h Code:

jniexport void Jnicall
java_com_libin_factory_ndk_ndk_ccallbackgetstring
        (jnienv *env, jclass type);

C + + code:

/**
* C code modulation Java class Setstrign (String s) method
* *
jniexport void Jnicall
Java_com_libin_factory_ndk_ndk_ Ccallbackgetstring (jnienv *env, Jclass type) {
    //Get byte code
    jclass jclazz = Env->findclass ("com/libin/factory/ Ndk/test "); Com.libin.factory.ndk.TestK point to Slip line
    //Get method
    Jmethodid jmethodids = Env->getmethodid (Jclazz, " Setstrings "," (ljava/lang/string;) V ");
    Instantiate the class of
    jobject jobj = Env->allocobject (jclazz);
    Call method
    jstring str = env->newstringutf ("Hello i am C, I am invoking Java method");

    Env->callvoidmethod (JOBJ,JMETHODIDS,STR);

}

Call:

Ndk.ccallbackgetstring ();

Show:
static Method C code-addint static (int a, int b) method in Java class

Java code:

    public static int addints (int a, int b) {
        int c = + b;
        Toast.maketext (Mainapplication.getcontext (), "calculated result::" +c, Toast.length_short). Show ();
        return a+b;
    }

. h Code:

jniexport void Jnicall
java_com_libin_factory_ndk_ndk_ccallbackaddints
        (jnienv *env, jclass type);

C + + code:

/**
* C code modulation Java class AddInt (int a, int b) static method
*/
jniexport void Jnicall
Java_com_libin_factory_ndk_ndk_ Ccallbackaddints (jnienv *env, Jclass type) {
    //Get byte code
    jclass jclazz = Env->findclass ("Com/libin/factory/ndk /test "); Com.libin.factory.ndk.TestK point to Slip line
    //Get method
    Jmethodid jmethodids = Env->getstaticmethodid (Jclazz, " Addints "," (II) I ");    //Instantiate the class  (static method does not need to instantiate the Class)
//    Jobject jobj = Env->allocobject (jclazz);
    Call method
    Jint value =env->callstaticintmethod (jclazz,jmethodids,99,1);
    LOGE ("value =%d\n", value);
}

Call:

Ndk.ccallbackaddints ();

Show:
static Method C code-SETSTRINGSS (String s) static method in Java class

Java code:

     public static void Setstringss (String s) {
        toast.maketext (Mainapplication.getcontext (), S, Toast.length_short). Show ();
    }

. h Code:

jniexport void Jnicall
java_com_libin_factory_ndk_ndk_ccallbackgetstrings
        (jnienv *env, jclass type);

C + + code:

/**
* C code modulation Java class SETSTRINGSS (String s) static method */
jniexport void Jnicall
Java_com_libin_factory_ndk_ Ndk_ccallbackgetstrings (jnienv *env, Jclass type) {
    //Get byte code
    jclass jclazz = Env->findclass ("com/libin/ Factory/ndk/test "); Com.libin.factory.ndk.TestK point to Slip line
    //Get method
    Jmethodid jmethodids = Env->getstaticmethodid (Jclazz, " Setstringss "," (ljava/lang/string;) V ");    //Instantiate the class (static method does not need to instantiate the Class)
//    Jobject jobj = Env->allocobject (jclazz);
    Call method
    jstring str = env->newstringutf ("Hello i am C, I'm calling Java static Method method");
    Env->callstaticvoidmethod (JCLAZZ,JMETHODIDS,STR);
}

Call:

Ndk.ccallbackgetstrings ();

Show:
object is called C-Tune Java create student information

Java code:
Student class

     public class Student {
    private String name;
    private int age;
    Private list<people> peoplelist;
    Public Student () {} public
    Student (String name, int age) {
        this.name = name;
        This.age = age;
    }
    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
        ' name---> ' + name + ' Age  ---> ' + age;
    }
}

. h Code:

 * * Class:     com_libin_factory_ndk_ndk
 * method:    getstudentinfo
 * Signature: () lcom/libin/ factory/ndk/student;
 * *
jniexport jobject jnicall java_com_libin_factory_ndk_ndk_getstudentinfo
  (jnienv *, jclass);

C + + code:

/**
 * Create student information * *
jniexport jobject jnicall java_com_libin_factory_ndk_ndk_getstudentinfo
( JNIEnv *env, Jclass type) {
    //com.libin.factory.ndk.student to put. Replace with///
    about package descriptor, this is com/libin/factory/ndk/ Student to use the full class name
    jclass stucls = Env->findclass ("com/libin/factory/ndk/student");//Student class reference

    // The constructor function of the type  named <init> return type must be void i.e. V
    jmethodid constrocmid = Env->getmethodid (Stucls, "<init> "," (ljava/lang/string;i) V ");

    jstring str = ENV->NEWSTRINGUTF ("Student named Aly");

    Jobject STU_OJB = Env->newobject (stucls, Constrocmid, str,);  Constructs an object, invokes the constructor of the class, and passes the argument return

    STU_OJB;

Call:

Toast.maketext (jniactivity.this, "Student info:" + ndk.getstudentinfo (). toString (), Toast.length_short). Show ();

Show:
C-Tune Java and new student information

Java code:
Student class

     public class Student {
    private String name;
    private int age;
    Private list<people> peoplelist;
    Public Student () {} public
    Student (String name, int age) {
        this.name = name;
        This.age = age;
    }
    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
        ' name---> ' + name + ' Age  ---> ' + age;
    }
}

. h Code:

 * * Class:     com_libin_factory_ndk_ndk
 * method:    updatestudentinfo
 * Signature: (lcom/libin/ factory/ndk/student;) lcom/libin/factory/ndk/student;
 * *
jniexport jobject jnicall java_com_libin_factory_ndk_ndk_updatestudentinfo
  (jnienv *, Jclass, jobject);

C + + code:

/**
 * With new student information * *
jniexport jobject jnicall
java_com_libin_factory_ndk_ndk_updatestudentinfo
        (jnienv *env, jclass type, Jobject obj) {

    jclass clazz;
    Jfieldid FID;
    Jstring J_str;
    Jstring J_newstr;

    Clazz = Env->getobjectclass (obj);
    if (clazz = = null) {return
        null;
    }
    FID = Env->getfieldid (clazz, "name", "ljava/lang/string;");
    if (clazz = = null) {return
        null;
    }
    J_str = (jstring) (env)->getobjectfield (obj, FID);
    J_newstr = (env)->newstringutf ("This is New Name hahah");
    (env)->setobjectfield (obj, FID, j_newstr);
    Deletes a local reference
    (env)->deletelocalref (clazz);
    (env)->deletelocalref (J_STR);
    (env)->deletelocalref (J_NEWSTR);
    return obj;

}

Call:

Toast.maketext (jniactivity.this, "Student info:" + ndk.updatestudentinfo (Ndk.getstudentinfo ()), Toast.length_short). Show ();

Show:

The next article we introduce as more than 2.2 configuration ndk--longitudinal enjoy silk slip
http://blog.csdn.net/github_33304260/article/details/71404510

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.