java native code调用_【学习Android NDK开发】native code通过JNI调用Java方法

1、建立Android应用

application name: CallJavaMethod

package name: com.example.cjm

main Activity: MainActivity

main Activity layout: activity_main

2、Java实现

打开layout/activity_main.xml布局文件,添加按钮控件,ID为“display_button_activity_main”

新建接口CJMListener,定义接口方法displaymessage

packagecom.example.cjm;public interfaceCJMListener {voiddisplayMessage(String message);

}

新建类CJM,实现接口CJMListener

定义native public方法displaySomething

packagecom.example.cjm;importandroid.os.Handler;public class CJM implementsCJMListener {static{

System.loadLibrary("cjm");

}privateHandler handler;privateCJMListener listener;publicCJM(CJMListener listener) {

handler= newHandler();this.listener =listener;

}

@Overridepublic void displayMessage(finalString message) {

handler.post(newRunnable() {

@Overridepublic voidrun() {

listener.displayMessage(message);

}

});

}public native voiddisplaySomething();

}

为MainActivity类,实现CJMListener,添加CMJ对象域

添加Button对象域,引用在布局文件中定义ID为“display_button_activity_main”的按钮控件

packagecom.example.cjm;importandroid.os.Handler;public class CJM implementsCJMListener {static{

System.loadLibrary("cjm");

}privateHandler handler;privateCJMListener listener;publicCJM(CJMListener listener) {

handler= newHandler();this.listener =listener;

}

@Overridepublic void displayMessage(finalString message) {

handler.post(newRunnable() {

@Overridepublic voidrun() {

listener.displayMessage(message);

}

});

}public native voiddisplaySomething();

}

当用户按下按钮,执行CJM对象的displaySomething方法

类CJM的displaySomething方法使用native关键字进行声明,将使用native code实现

实现上,在nativie code中,会回调CJM对象的displayerMessage方法,并传递String类型的消息,用于显示

注意,在CJM类中displayerMessage的实现,由于native code并不是在主线程中执行,所以使用了Android的Handler执行MainActivity的方法

3、C实现

使用javah为CJM的native方法生成头文件(步骤省略……)

新建.c文件,实现该头文件的方法

#include "com_example_cjm_CJM.h"JNIEXPORTvoidJNICALL Java_com_example_cjm_CJM_displaySomething

(JNIEnv*env, jobject thiz) {

jclass ClassCJM= (*env)->FindClass(env, "com/example/cjm/CJM");

jmethodID MethodDisplayMessage= (*env)->GetMethodID(env, ClassCJM, "displayMessage", "(Ljava/lang/String;)V");

jstring value= (*env)->NewStringUTF(env, "Hello World!");

(*env)->CallVoidMethod(env, thiz, MethodDisplayMessage, value);

}

JNI调用Java方法的步骤:

1) 获取jmethodID;

GetMethodID

jmethodID GetMethodID(JNIEnv *env, jclass clazz,

const char *name, const char *sig);

Returns the method ID for an instance (nonstatic) method of a class or interface. The method may be defined in one of the clazz’s superclasses and inherited by clazz. The method is determined by its name and signature.

GetMethodID() causes an uninitialized class to be initialized.

To obtain the method ID of a constructor, supply as the method name and void (V) as the return type.

2) 调用Java方法;

NativeType CallMethod(JNIEnv *env, jobject obj,

jmethodID methodID, ...);

Methods from these three families of operations are used to call a Java instance method from a native method.They only differ in their mechanism for passing parameters to the methods that they call.

These families of operations invoke an instance (nonstatic) method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling GetMethodID().

When these functions are used to call private methods and constructors, the method ID must be derived from the real class of obj, not from one of its superclasses.

Instance Method Calling Routines:

CallVoidMethod

void

CallObjectMethod

jobject

CallBooleanMethod

jboolean

CallByteMethod

jbyte

CallCharMethod

jchar

CallShortMethod

jshort

CallIntMethod

jint

CallLongMethod

jlong

CallFloatMethod

jfloat

CallDoubleMethod

jdouble

运行结果截图:

fad28d31cafbf060971c51594d9c72d7.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值