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)
/* 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
#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:
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;}
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:
-
- Gets the method ID of the class
- 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
-
- Gets the ID of the variable for the object
- Access variables based on object and variable ID
Android JNI C + + layer calls Java