How Android NDK avoids the JAVA_XXX standard definition

Source: Internet
Author: User

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

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.