1. Write native method (JAVA2C) and non-native method (C2java):
PackageCom.example.provider; Public classCallbackjava {//C calling Java Null method Public voidHellofromjava () {System.out.println ("Hello from Java"); } //C Calling a method with two int parameters in Java Public intADD (intXinty) {intresult = x +y; System.out.println ("Java result=" +result); returnresult; } //C Calling a method in Java with parameter string Public voidprintstring (String s) {System.out.println ("Java" +s); } //Static Methods Public Static voidPrintstaticstr (String s) {System.out.println ("Java static->" +s); } //let C code call the corresponding Java code Public native voidCallhellofromjava (); Public native voidCalladd (); Public native voidcallprintstring (); //call a static C code Public native voidcallprintstaticstr ();}
2, through the Javah command generation signature, through the JAVAP command production native method signature:
3. C Code
Findclass: Find the class file in Java
Getmethodid: Finds the method to invoke based on the class and method signature.
#include <stdio.h>#include<jni.h>#include<malloc.h>#include"com_example_provider_callbackjava.h"; #include<android/log.h>#defineLog_tag "System.out.c"#defineLOGD (...) __android_log_print (Android_log_debug, Log_tag, __va_args__)#defineLogi (...) __android_log_print (Android_log_info, Log_tag, __va_args__)jmethodid Getmethodid (jnienv* Env,Char* MethodName,Char*signname) {Jclass Jclazz= (*env)->findclass (env,"Com/example/provider/callbackjava"); if(Jclazz = =0) {LOGD ("Class not find"); return; } //2, find the ID of the method in class, the third parameter method name, the fourth parameter the method corresponding to the signatureJmethodid Methodid = (*env)Getmethodid (env, Jclazz, MethodName, Signname); if(Methodid = =0) {LOGD ("Method Not find"); return; } returnMethodid;} Jniexportvoidjnicall Java_com_example_provider_callbackjava_callhellofromjava (jnienv*env, Jobject obj) { //1. Find the class file in Java//The second parameter is the class name (package name)Jclass Jclazz = (*env)->findclass (env,"Com/example/provider/callbackjava"); if(jclazz==0) {LOGD ("Class not find"); return; } //2, find the ID of the method in class, the third parameter method name, the fourth parameter the method corresponding to the signatureJmethodid Methodid = (*env)->getmethodid (env, Jclazz,"Hellofromjava","() V"); if(methodid==0) {LOGD ("Hellofromjava Method Not find"); return; } //3. Call Method//void (*callvoidmethod) (jnienv*, Jobject, Jmethodid, ...);(*env)Callvoidmethod (env,obj,methodid);} Jniexportvoidjnicall Java_com_example_provider_callbackjava_calladd (jnienv*env, Jobject obj) {Jmethodid Mid= Getmethodid (env,"ADD","(II) I"); intresult = (*env)->callintmethod (env, obj, Mid,3,6); LOGD ("C to Java add:d%", result);} Jniexportvoidjnicall java_com_example_provider_callbackjava_callprintstring (jnienv*env, Jobject obj) {LOGD ("C to Java callprintstring start"); Jmethodid Mid= Getmethodid (env,"printstring","(ljava/lang/string;) V"); (*ENV)->callvoidmethod (env, obj, Mid, (*env)->newstringutf (env,"Call printstring Finish")); LOGD ("C to Java callprintstring finish");} Jniexportvoidjnicall java_com_example_provider_callbackjava_callprintstaticstr (jnienv*env, Jobject obj) {Jclass Jclazz= (*env)->findclass (env,"Com/example/provider/callbackjava"); if(Jclazz = =0) {LOGD ("Class not find"); return; } //static method calls Getstaticmethodid and CallstaticxxxmethodJmethodid Methodid = (*env)->getstaticmethodid (env, Jclazz,"Printstaticstr","(ljava/lang/string;) V"); if(Methodid = =0) {LOGD ("Printstaticstr Method Not find"); return; } (*ENV)->callstaticvoidmethod (env, Jclazz, Methodid, (*env)->newstringutf (env,"static Hello Java"));}