Android(安卓)NDK c调用java代码
16lz
2022-05-16
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">一、首先是几个重要的方法:</span>
//全局的变量,用来记录java中的类/对象/方法ID
jclass m_class;
jobject m_object;
jmethodID m_mid_static,m_mid;
//得到传入对象所对应的类
jclass clazz=(*env)->GetObjectClass(env,jobject);
//得到全局可用的类,
m_class=(jclass)(*env)->NewGlobalRef(env,jclass);
//得到全局可用的对象
m_object=(jobject)(*env)->NewGlobalRef(env,jobject);
//得到java中静态方法的id,后面的2个参数为在java中的方法名、方法中参数的类型(后面举例说明)
m_mid_static=(*env)->GetStaticMethodID(env,m_class,"method_name_in_java","args");
//得到java中方法的id.....
m_mid=(*env)->GetMethodID(env,m_class,"method_name_in_java","args");
//执行java中静态的空返回值的类型的方法,因为是静态方法,所以要指定java类,方法id以及要传入的参数
(*env)->CallStaticVoidMethod(env,m_class,m_mid_static,args...);
//执行java中空返回值类型的方法,需要传入java对象,方法id以及要传入的参数
(*env)->CallVoidMethod(env,m_object,m_mid,args...);
二、示例代码
native代码类:
package com.aii.ndk;import android.app.Activity;import android.os.Handler;import android.os.Message;public class CounterNative {<span style="white-space:pre"></span>public static Handler handler;<span style="white-space:pre"></span>public CounterNative(final Activity activity) {<span style="white-space:pre"></span>handler = new Handler() {<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public void handleMessage(Message msg) {<span style="white-space:pre"></span>activity.setTitle(msg.obj.toString());<span style="white-space:pre"></span>}<span style="white-space:pre"></span>};<span style="white-space:pre"></span>// 初始化该类的时候,在本地方法执行初始化,具体内容是:将当前对象,类,方法等记录在c的全局变量中<span style="white-space:pre"></span>nativeSetup();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>static {<span style="white-space:pre"></span>//加载库<span style="white-space:pre"></span>System.loadLibrary("CCallJava");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>//由c语言回调的方法,一旦回调,则发送信息给主线程的looper,要求改变UI<span style="white-space:pre"></span>public static void setValue(int value) {<span style="white-space:pre"></span>Message msg = Message.obtain();<span style="white-space:pre"></span>msg.obj = value;<span style="white-space:pre"></span>handler.sendMessage(msg);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>//同上<span style="white-space:pre"></span>public void setV(int value) {<span style="white-space:pre"></span>Message msg = Message.obtain();<span style="white-space:pre"></span>msg.obj = value;<span style="white-space:pre"></span>handler.sendMessage(msg);<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>public native void nativeSetup();<span style="white-space:pre"></span>public static native void nativeExecute(int n);<span style="white-space:pre"></span>public native void nativeExec(int n);}
mainActivity类:
package com.example.ccalljava;import com.aii.ndk.CounterNative;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;public class MainActivity extends ActionBarActivity {private CounterNative cNative;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 初始化对象cNative = new CounterNative(this);}// 该方法由UI上的的按钮触发public void staticClick(View view) {// 调用本地方法,CounterNative.nativeExecute(10);}// 该方法由UI上的的按钮触发public void click(View view) {// 调用本地方法,cNative.nativeExec(100);}}
重点:.c类
#include "com_aii_ndk_CounterNative.h"#include<android/log.h>//记录类对象jclass m_class;//记录对象对象jobject m_object;//记录方法idjmethodID m_mid_static, m_mid;/*这段注释是.h文件中自动生成拷过来的,其中的signature经过测试。。()V表示的应该是无参方法。(I)V表示的应该是参数的额1个int的方法,,,猜的* Class: com_aii_ndk_CounterNative* Method: nativeSetup* Signature: ()V**/JNIEXPORT void JNICALL Java_com_aii_ndk_CounterNative_nativeSetup(JNIEnv *env,<span style="white-space:pre"></span>jobject thiz) {<span style="white-space:pre"></span>//传过来的是jobject类型对象,现在要得到jclass类型,<span style="white-space:pre"></span>jclass clazz = (*env)->GetObjectClass(env, thiz);<span style="white-space:pre"></span>//得到全局可用的<span style="white-space:pre"></span>m_class = (jclass) (*env)->NewGlobalRef(env, clazz);<span style="white-space:pre"></span>//得到全局可用的<span style="white-space:pre"></span>m_object = (jobject) (*env)->NewGlobalRef(env, thiz);<span style="white-space:pre"></span>//得到方法的id<span style="white-space:pre"></span>m_mid_static = (*env)->GetStaticMethodID(env, m_class, "setValue", "(I)V");<span style="white-space:pre"></span>m_mid = (*env)->GetMethodID(env, m_class, "setV", "(I)V");}/** Class: com_aii_ndk_CounterNative* Method: nativeExecute* Signature: (I)V***/JNIEXPORT void JNICALL Java_com_aii_ndk_CounterNative_nativeExecute(<span style="white-space:pre"></span>JNIEnv *env, jclass clazz, jint arg) {<span style="white-space:pre"></span>//调用静态的方法,肯定得知道方法所属的字节码,方法的id(这里的唯一标识用这个表示),方法的参数,然后通过这个*env去虚拟机找去<span style="white-space:pre"></span>(*env)->CallStaticVoidMethod(env, clazz, m_mid_static, arg);}/** Class: com_aii_ndk_CounterNative* Method: nativeExec* Signature: (I)V***/JNIEXPORT void JNICALL Java_com_aii_ndk_CounterNative_nativeExec(JNIEnv *env,<span style="white-space:pre"></span>jobject obj, jint arg) {<span style="white-space:pre"></span>(*env)->CallVoidMethod(env, obj, m_mid, arg);}
顺便贴上布局文件:
<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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="staticClick" android:text="static method" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="method" /></LinearLayout>
更多相关文章
- android 数据传输之JSON
- Android的Message机制
- Binder 简介
- Bitmap和Drawable相互转换方法
- Android(安卓)EventBus 源码解析
- android onSaveInstanceState方法 横坚屏切换
- 浅谈Java中Collections.sort对List排序的两种方法
- 类和 Json对象
- Python list sort方法的具体使用