Android JNI C + + layer calls Java

Source: Internet
Author: User

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

To call Java layer code from the C + + layer step: 1. To create a Java method and a local method in a Java class
public class testndk{    int a;//The Java variable that will be modified in this example    Handler Handler;    Public testndk (Handler Handler) {        This.handler = Handler;        SetUp ();    }    Public native void setUp ()//local method public    native static int getstringfromnative ();//local method public    int  Geta () {return A;} public native void SetA (int a);//local method public void Notifyfiledchange () {/////This example will be called by C + + Java method Message message = new message (); Bundle bundle = new bundle (); Bundle.putint ("A", a); Message.setdata (bundle); Message.what=1; Handler.sendmessage (message); } static {system.loadlibrary ("Myjni");//import generated link library file }}        

2. Under the Jni folder, create the. h file for the class (you can generate the. h file for the class by using the Javah command, and do not repeat it) to create a. cpp file (code for writing A/C + + layer)
    • . h file:
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class COM_X_MP4PLAYER_TESTNDK */#ifndef _included_com_x_mp4player_testndk#define _included_com_x_mp4player_testndk#ifdef __ Cplusplusextern "C" {#endif */* Class:     com_x_mp4player_testndk * Method:    setUp * Signature: () V */ jniexport void Jnicall java_com_x_mp4player_testndk_setup (jnienv *, jobject);/* * Class:com_x_mp4player_ TESTNDK * Method:seta * Signature: (I) V */jniexport void jnicall Java_com_x_mp4player_testndk_seta (jnienv *
            
             env, Jobject thiz,jint i);/* * CLASS:COM_X_MP4PLAYER_TESTNDK * method:getstringfromnative * Signature: () i */
             JNIEX PORT jint jnicall java_com_x_mp4player_testndk_getstringfromnative (jnienv *, Jclass); #ifdef __cplusplus} #endif # endif  
                    
    • . cpp Files
#include <jni.h>#include <com_x_mp4player_TestNdk.h>jobject m_object;jmethodid m_mid;jfieldid m _fid; Jniexport jint jnicall java_com_x_mp4player_testndk_getstringfromnative        (jnienv *env, Jclass CLS) {    return 1;} jniexport void jnicall java_com_x_mp4player_testndk_setup        (jnienv *env, jobject thiz) {Jclass clazz = (*env) . Getobjectclass (thiz);//Gets the class M_object = (*env) of the object. Newglobalref (thiz);//Create local variable M_mid = (*env) of the object. Getmethodid (Clazz, "Notifyfiledchange", "() V");//Gets the Java method's id M_fid = (*env). Getfieldid (Clazz, "a", "I");//Get the ID of the Java variable return;} jniexport void Jnicall Java_com_x_mp4player_testndk_seta (jnienv *env, jobject thiz,jint i) {(*env). Setintfield (M_object, m_fid,i); (*env). Callvoidmethod (M_object,m_mid); return;}           

  

3. Calling the Java method requires the object of the class

There are two ways to get the objects of a class in the C + + layer:

    • Calling a local method through the Java layer (such as the Setup () method in the example), when called, passes in the object jobject or the object's class Jclass

jniexport void jnicall java_com_x_mp4player_testndk_setup        (jnienv *env, jobject thiz) {
... Jclass clazz = (*env). Getobjectclass (thiz);//Gets the class of the object ... return;}
    • Create a Java object from C + +

Here's how:

1. Creating a homogeneous object from a native method of the Java layer

Steps:

I. Getting classes from objects

Ii. getting the ID of a class's constructor through a class

Iii. creating a new object based on the method ID and class

void Jnicall Java_nativemethod         *env, jobject thiz,jint i) {    ...     = (*env). Getobjectclass (thiz);     = (*env). Getmethodid (Clazz,"<init>","() V");     = (*env). NewObject (clazz,mid);    ...     return ;}

2. Create different classes of objects through C + +

Steps:

I. Obtaining the required classes through the Findclass method

Ii. getting the ID of a class's constructor through a class

Iii. creating a new object based on the method ID and class

void Jnicall Java_nativemethod         *env, jobject thiz,jint i) {    ...     = (*env). Findclass ("com/x/test/test"); // parameter is a class path    Jmethodid mid = (*env). Getmethodid (Clazz,"<init>","() V") ;     = (*env). NewObject (clazz,mid);    ...     return ;}

4. Steps to invoke the Java method:
      1. Gets the method ID of the class
      2. Calling Java methods based on object and method IDs

    • In the example, after the Java call to the local method setup, call the Local method Seta (int i) and call the Java Method Notifyfiledchange () method in Local method Seta (int i)

5.c/c++ direct access to Java variables

      1. Gets the ID of the variable for the object
      2. Access variables based on object and variable ID

Android JNI C + + layer calls Java

Related Article

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.