Android JNI Java is symmetric associated with C + + objects (JNI optimized design to ensure the stability of JNI calls)

Source: Internet
Author: User

Reprint please declare: Original translation from: http://www.cnblogs.com/xiezie/p/5930503.html

Android JNI Java and C + + objects establish symmetric Association 1. Java objects hold pointers to C + + objects
    1. Create a variable of type int (such as int mobj) in the Java class to store pointers to C + + objects
    2. In a local method that creates a C + + object (such as Mycplusobj), a pointer to a C + + object is stored in a Java variable (mobj)
 jniexport void   Jnicall Java_nativemethod ( jnienv  *env, Jobject thiz) {mycplusobj  *obj = new   Mycplusobj (); Jclass clazz  = (jclass) (*env).    Getobjectclass (Thiz); Jfieldid FID  = (jfieldid) (*env). Getfieldid (Clazz,  " mobj   ", "  i   ); ( *env). Setintfield (Thiz, FID, (jint) obj);}  
    • If you want to get a C + + object, get a pointer to the C + + object through Mobj
      Jclass Objclazz = (jclass) env->getobjectclass (obj); // obj is the corresponding Java object " Mobj " " I "  = (jlong) env->*cplusobj = (mycplusobj *) p; // cplusobj C + + objects that correspond to Java objects

2.c++ object holds a reference to a Java object
    1. Create a variable of type jint (such as Jint mobj) in a C + + class to store a reference to a Java object
    2. Storing a reference to a Java object when creating a C + + object
      Jobject gthiz = (jobject) env->newglobalref (thiz); // thiz for Java objects (*obj). Javaobj = (jint) gthiz;

Android JNI Java is symmetric associated with C + + objects (JNI optimized design to ensure the stability of JNI calls)

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.