博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【学习Android NDK开发】native code通过JNI调用Java方法
阅读量:6934 次
发布时间:2019-06-27

本文共 3917 字,大约阅读时间需要 13 分钟。

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

package com.example.cjm;public interface CJMListener {    void displayMessage(String message);}

 

新建类CJM,实现接口CJMListener

定义native public方法displaySomething

package com.example.cjm;import android.os.Handler;public class CJM implements CJMListener {        static {        System.loadLibrary("cjm");    }        private Handler handler;    private CJMListener listener;        public CJM(CJMListener listener) {        handler = new Handler();        this.listener = listener;    }    @Override    public void displayMessage(final String message) {        handler.post(new Runnable() {            @Override            public void run() {                listener.displayMessage(message);            }        });    }        public native void displaySomething();}

 

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

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

package com.example.cjm;import android.os.Handler;public class CJM implements CJMListener {        static {        System.loadLibrary("cjm");    }        private Handler handler;    private CJMListener listener;        public CJM(CJMListener listener) {        handler = new Handler();        this.listener = listener;    }    @Override    public void displayMessage(final String message) {        handler.post(new Runnable() {            @Override            public void run() {                listener.displayMessage(message);            }        });    }        public native void displaySomething();}

 

当用户按下按钮,执行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"JNIEXPORT void JNICALL 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 <init> as the method name and void (V) as the return type.

2) 调用Java方法;

NativeType Call<type>Method(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

 

 

运行结果截图:

 

 

转载地址:http://vhgjl.baihongyu.com/

你可能感兴趣的文章
C#中字符串的处理
查看>>
循序渐进 OSPF的详细剖析(三)
查看>>
linux shell脚本之lnmp的搭建
查看>>
rsyslog+loganalyzer+evtsys搭建集中式监控系统
查看>>
[Unity3d]制作打包并载入AssetBundle
查看>>
Excel直接转图片
查看>>
iOS静态库的编译
查看>>
JAVA基础中关于double进制问题的解析
查看>>
Android 图片透明度处理代码
查看>>
邮件服务器问题--邮件积压、传递延迟解决方法
查看>>
Maven2整合集成IntelliJ IDEA创建Web项目
查看>>
实战postfix邮件发送
查看>>
U盘如何量产成USB-CDROM
查看>>
shell批量增删改查百库百表(mysql)
查看>>
网路游侠:日志审计系统与SOC的区别
查看>>
无处不在的网络与中国IPv9
查看>>
hexo
查看>>
云场景实践研究第85期:墨迹天气
查看>>
一个SAP开发人员的2017总结
查看>>
7216:Minecraft
查看>>