Android C code callback java method, android callback

Source: Internet
Author: User

Android C code callback java method, android callback

This article describes the following three C code callback java methods:

1. c code callback java empty 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 are similar:

① Locate the bytecode object

// Jclass (* FindClass) (JNIEnv *, const char *);

// The path of the class to which the second parameter calls back the java method "com/itheima/callbackjava/JNI"

② Locate the method object through the bytecode object

// JmethodID (* GetMethodID) (JNIEnv *, jclass, const char *, const char *);

Second parameter bytecode object third parameter java method to be reflected call name fourth parameter to be reflected call java method Signature

Javap-s: Project/bin/classes of the full Class Name of the class for which the method signature is to be obtained

③ Create a java object using bytecode (optional) if the local Method and the java Method to be called back are in the same class, you can directly call the created Method using 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 must manually create a java object through the newly created bytecode object.

Then the method for calling java back and forth through this object

Note that if you create an activity object callback method that contains the context, this method will not work !!! Return NULL pointer exception

④ Call the java method through reflection

// Void (* CallVoidMethod) (JNIEnv *, jobject, jmethodID ,...);

The second parameter calls the java method object. The third parameter is the jmethodID object to call. The optional parameter is the parameter received when the method is called.

Next, practical project implementation

Create a project with the following layout:

<LinearLayout xmlns: 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 "> <Button android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: onClick =" callbackvoid "android: text = "Callback null method"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "callbackint" android: text = "Callback int Parameter Method"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "callbackString" android: text = "Callback String parameter method"/> </LinearLayout>

Create JNI. java

Package com. wuyudong. callbackjava; public class JNI {static {System. loadLibrary ("callback");} public native void callbackvoidmethod (); public native void callbackintmethod (); public native void callbackStringmethod (); // C calls the java empty method public void helloFromJava () {System. out. println ("hello from java");} // C calls the public int add (int x, int y) {return x + y;} method with two int parameters in Java ;} // C calls the public void printString (String s) {System. out. println (s );}}

MainActivity. java

package com.wuyudong.callbackjava;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;public class MainActivity extends Activity {        JNI jni;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        jni = new JNI();    }    public void callbackvoid(View v) {        jni.callbackvoidmethod();    }            public void callbackint(View v) {        jni.callbackintmethod();    }        public void callbackString(View v) {        jni.callbackStringmethod();    }}

Callback. c

# Include <jni. h> # include <android/log. h> # define LOG_TAG "System. out "# define LOGD (...) _ android_log_print (ANDROID_LOG_DEBUG, LOG_TAG, _ VA_ARGS _) # define LOGI (...) _ android_log_print (delimiter, LOG_TAG, _ VA_ARGS _) JNIEXPORT void JNICALL delimiter (JNIEnv * env, jobject clazz) {// 1. Get the bytecode object jclass (* FindClass) (JNIEnv *, const char *); jclass claz = (* Env)-> FindClass (env, "com/wuyudong/callbackjava/JNI"); // 2. Obtain the Method object jmethodID (* GetMethodID) (JNIEnv *, jclass, const char *, const char *); jmethodID methodID = (* env)-> GetMethodID (env, claz, "helloFromJava", "() V "); // 3. Create an Object (that is, clazz) through the bytecode Object // 4. Call the method (* env) through the Object-> CallVoidMethod (env, clazz, methodID );} JNIEXPORT void JNICALL Java_com_wuyudong_callbackjava_JNI_callbackintmethod (JNIEnv * env, j Object clazz) {// 1. Obtain the bytecode object jclass (* FindClass) (JNIEnv *, const char *); jclass claz = (* env)-> FindClass (env, "com/wuyudong/callbackjava/JNI"); // 2. Obtain the Method object jmethodID (* GetMethodID) (JNIEnv *, jclass, const char *, const char *); jmethodID methodID = (* env)-> GetMethodID (env, claz, "add", "(II) I "); // 3. Create an Object (that is, clazz) through the bytecode Object // 4. Call the method int result = (* env) through the Object-> CallIntMethod (env, clazz, methodID, 3, 5 ); LOGD ("result = % d", result);} JNIEXPORT void JNICALL Java_com_wuyudong_callbackjava_JNI_callbackStringmethod (JNIEnv * env, jobject clazz) {// 1. Obtain the bytecode object jclass (* FindClass) (JNIEnv *, const char *); jclass claz = (* env)-> FindClass (env, "com/wuyudong/callbackjava/JNI"); // 2. Obtain the Method object jmethodID (* GetMethodID) (JNIEnv *, jclass, const char *, const char *); jmethodID methodID = (* env)-> GetMethodID (env, c Laz, "printString", "(Ljava/lang/String;) V"); // 3. Create an Object (that is, clazz) through a bytecode Object) // 4. Call the method through an object // char * str = "hello from c! "; Jstring result = (* env)-> NewStringUTF (env," hello from c "); (* env)-> CallVoidMethod (env, clazz, methodID, result );}

To learn more about C language and algorithms, follow the Public Account "csuanfa"

Related Article

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.