【学习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”

<RelativeLayout 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" >

    <Button
        android:id="@+id/display_button_activity_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Display"
        tools:context=".MainActivity" />

</RelativeLayout>

新建接口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

运行结果截图:

 

转载于:https://www.cnblogs.com/dyingbleed/archive/2012/10/12/2721781.html

猜你喜欢

转载自blog.csdn.net/weixin_34417814/article/details/93301826