Java code: public class messageutil {static{system.loadlibrary ("SMS");} Public static native void showtoast (object context,string message);} JNI code: File -- sms.h//// created by mrper on 2015/8/2.//#include < Jni.h> #ifndef sender_sms_h#define sender_sms_hvoid showtoast (jnienv *env,jclass Clazz,jobject handler,jstring message); Static jint registenativemethods (JNIEnv *env, Const char* classname,jninativemethod *methods,int nummethod);static jint Registenatives (jnienv *env); #endif //sender_sms_h file -- sms.cpp//// created by mrper on 2015/8/2.//#include "Sms.h" #include <stdio.h>static jninativemethod nativemethods[] = {{"Showtoast", "(Ljava/lang/object; ljava/lang/string;) V ", (void*) showtoast}};void showtoast (JNIENV&NBSP;*ENV,JCLASS&NBSP;CLAZZ,JOBJECT&NBsp;handler,jstring message) {jclass cls_toast = env->findclass ("android/widget/Toast"); Jmethodid mid_showtoast = env->getstaticmethodid (Cls_toast, "MakeText", "(Landroid/content/ Context; Ljava/lang/charsequence;i) landroid/widget/toast; "); Jobject obj_toast = env->callstaticobjectmethod (cls_toast,mid_showtoast,handler,message,1000 ); Jmethodid mid_show = env->getmethodid (Cls_toast, "Show", "() V"); Env->callvoidmethod (obj_ Toast,mid_show);} Static jint registenativemethods (Jnienv *env,const char* classname,jninativemethod *methods,int nummethod) {jclass clazz;clazz = env->findclass (className);if ( Clazz == null) return JNI_FALSE;if (env->registernatives (Clazz,methods,nummethod) < 0) {return jni_false;} Return jni_true;} Static jint registenatives (jnienv *env) {if (Registenativemethods (env), "Com/mrper/sender/messageutil ", nativemethods,sizeof (NativeMethods)/sizeof (Nativemethods[0]) ) != jni_true ) {Return jni_false;} Return jni_true;} Jint jni_onload (javavm *vm, void*reserved) {jint result = -1; Jnienv *env;vm->getenv ((void**) &env,jni_version_1_6);if (env == null) {goto Fail;} if (registenatives (env) != jni_true) {goto fail;} Result = jni_version_1_6;fail:return result;}
How Android NDK avoids the JAVA_XXX standard definition