Android C Code Callback Java method

Source: Internet
Author: User

This article describes the following three C code callback Java methods

1. C code callback Java NULL method

2. C code callback Java int type parameter method

3. C code callback javastring type parameter method

Methods are similar, first look at the C code callback Java empty method, the other two kinds of:

① Find Bytecode Object

Jclass (*findclass) (jnienv*, const char*);

The second parameter is the path to the class of the Java method where the callback is "Com/itheima/callbackjava/jni"

② finding a method object from a byte-code object

Jmethodid (*getmethodid) (jnienv*, Jclass, const char*, const char*);

The second parameter bytecode object The third parameter to reflect the calling Java method name fourth parameter to reflect the calling Java method signature

Javap-s the full class name of the class to get the method signature/bin/classes run JAVAP

③ creating Java objects by bytecode (optional) if the local method and the Java method to be recalled are in the same class, the created method can be invoked directly from the Java object passed by JNI.

Jobject obj = (*env)->allocobject (Env,claz);

When the callback method is not in the same class as the local method, you need to manually create a Java object from the byte-code object you just created.

The way to tune Java back through this object

It is important to note that if you are creating a callback method for an Activity object that also contains context this method does not work!!! return NULL pointer exception

④ Reflection calls a Java method

void (*callvoidmethod) (jnienv*, Jobject, Jmethodid, ...);

The second parameter calls the Java method's object the third parameter to call the Jmethodid object optional parameter that is received when the method is called

Then the actual project implementation

Create a new project with the following layout:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "Callbackvoid"Android:text= "Callback null method" />    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "Callbackint"Android:text= "Callback int parameter method" />    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "Callbackstring"Android:text= "Callback string parameter method" /></LinearLayout>

New Jni.java

 PackageCom.wuyudong.callbackjava; Public classJNI {Static{system.loadlibrary ("Callback"); }     Public native voidCallbackvoidmethod ();  Public native voidCallbackintmethod ();  Public native voidCallbackstringmethod (); //C calling Java Null method     Public voidHellofromjava () {System.out.println ("Hello from Java"); }    //C Calling a method with two int parameters in Java     Public intAddintXinty) {returnX +y; }    //C Calling a method in Java with parameter string     Public voidprintstring (String s) {System.out.println (s); }}

Mainactivity.java

 PackageCom.wuyudong.callbackjava;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View; Public classMainactivityextendsActivity {JNI jni; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); JNI=NewJNI (); }     Public voidcallbackvoid (View v) {jni.callbackvoidmethod (); }             Public voidCallbackint (View v) {jni.callbackintmethod (); }         Public voidcallbackstring (View v) {jni.callbackstringmethod (); }}

Callback.c

#include <jni.h>#include<android/log.h>#defineLog_tag "System.out"#defineLOGD (...) __android_log_print (Android_log_debug, Log_tag, __va_args__)#defineLogi (...) __android_log_print (Android_log_info, Log_tag, __va_args__)Jniexportvoidjnicall Java_com_wuyudong_callbackjava_jni_callbackvoidmethod (jnienv*env, Jobject clazz) {    //1. Get the byte code object Jclass (*findclass) (jnienv*, const char*);Jclass Claz = (*env)->findclass (env,"Com/wuyudong/callbackjava/jni"); //2, Get Method Object Jmethodid (*getmethodid) (jnienv*, Jclass, const char*, const char*);Jmethodid Methodid = (*env)->getmethodid (env, Claz,"Hellofromjava","() V"); //3. Create an object from the bytecode object (that is, clazz)//4. Calling methods by Object(*env)Callvoidmethod (env, Clazz, Methodid);} Jniexportvoidjnicall Java_com_wuyudong_callbackjava_jni_callbackintmethod (jnienv*env, Jobject clazz) {    //1. Get the byte code object Jclass (*findclass) (jnienv*, const char*);Jclass Claz = (*env)->findclass (env,"Com/wuyudong/callbackjava/jni"); //2, Get Method Object Jmethodid (*getmethodid) (jnienv*, Jclass, const char*, const char*);Jmethodid Methodid = (*env)->getmethodid (env, Claz,"Add","(II) I"); //3. Create an object from the bytecode object (that is, clazz)//4. Calling methods by Object    intresult = (*env)->callintmethod (env, Clazz, Methodid,3,5); LOGD ("result =%d", result);} Jniexportvoidjnicall Java_com_wuyudong_callbackjava_jni_callbackstringmethod (jnienv*env, Jobject clazz) {    //1. Get the byte code object Jclass (*findclass) (jnienv*, const char*);Jclass Claz = (*env)->findclass (env,"Com/wuyudong/callbackjava/jni"); //2, Get Method Object Jmethodid (*getmethodid) (jnienv*, Jclass, const char*, const char*);Jmethodid Methodid = (*env)->getmethodid (env, Claz,"printstring",            "(ljava/lang/string;) V"); //3. Create an object from the bytecode object (that is, clazz)//4. Calling methods by Object//char* str = "Hello from c!";jstring result = (*env)->newstringutf (env,"Hello from C"); (*ENV)Callvoidmethod (env, Clazz, methodid, result);}

Get more knowledge of C language and algorithms, follow the public number: "Csuanfa"

Android C Code Callback Java method

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.