本篇将介绍在JNI编程中C调用Java实现。
源码下载地址:http://download.csdn.net/detail/xyz_lmn/4868265
关键代码:
java:
- public class CCallJava {
- public static String getTime() {
- Log.d("CCallJava", "Call From C Java Static Method" +String.valueOf(System.currentTimeMillis()));
- return String.valueOf(System.currentTimeMillis());
- }
- public void sayHello(String msg) {
- Log.d("CCallJava", "Call From C Java void Method" +String.valueOf(System.currentTimeMillis()));
- }
- }
C:
- #include "TestCCallJava.h"
- #include <android/log.h>
- extern JNIEnv* jniEnv;
- jclass TestCCallJava;
- jobject mTestCCallJava;
- jmethodID getTime;
- jmethodID sayHello;
- int GetTestCCallJavaInstance(jclass obj_class);
- /**
- * 初始化 类、对象、方法
- */
- int InitTestCCallJava() {
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "InitTestCCallJava Begin 1" );
- if(jniEnv == NULL) {
- return 0;
- }
- if(TestCCallJava == NULL) {
- TestCCallJava = (*jniEnv)->FindClass(jniEnv,"com/trunkbow/ccalljava/CCallJava");
- if(TestCCallJava == NULL){
- return -1;
- }
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "InitTestCCallJava Begin 2 ok" );
- }
- if (mTestCCallJava == NULL) {
- if (GetTestCCallJavaInstance(TestCCallJava) != 1) {
- (*jniEnv)->DeleteLocalRef(jniEnv, TestCCallJava);
- return -1;
- }
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "InitTestCCallJava Begin 3 ok" );
- }
- if (getTime == NULL) {
- getTime = (*jniEnv)->GetStaticMethodID(jniEnv, TestCCallJava, "getTime","()Ljava/lang/String;");
- if (getTime == NULL) {
- (*jniEnv)->DeleteLocalRef(jniEnv, TestCCallJava);
- (*jniEnv)->DeleteLocalRef(jniEnv, mTestCCallJava);
- return -2;
- }
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "InitTestCCallJava Begin 4 ok" );
- }
- if (sayHello == NULL) {
- sayHello = (*jniEnv)->GetMethodID(jniEnv, TestCCallJava, "sayHello","(Ljava/lang/String;)V");
- if (sayHello == NULL) {
- (*jniEnv)->DeleteLocalRef(jniEnv, TestCCallJava);
- (*jniEnv)->DeleteLocalRef(jniEnv, mTestCCallJava);
- (*jniEnv)->DeleteLocalRef(jniEnv, getTime);
- return -3;
- }
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "InitTestCCallJava Begin 5 ok" );
- }
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "InitTestCCallJava Begin 6" );
- return 1;
- }
- int GetTestCCallJavaInstance(jclass obj_class) {
- if(obj_class == NULL) {
- return 0;
- }
- jmethodID construction_id = (*jniEnv)->GetMethodID(jniEnv, obj_class,
- "<init>", "()V");
- if (construction_id == 0) {
- return -1;
- }
- mTestCCallJava = (*jniEnv)->NewObject(jniEnv, obj_class,
- construction_id);
- if (mTestCCallJava == NULL) {
- return -2;
- }
- return 1;
- }
- /**
- * 获取时间 ---- 调用 Java 方法
- */
- void GetTime() {
- if(TestCCallJava == NULL || getTime == NULL) {
- int result = InitTestCCallJava();
- if (result != 1) {
- return;
- }
- }
- jstring jstr = NULL;
- char* cstr = NULL;
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "GetTime Begin" );
- jstr = (*jniEnv)->CallStaticObjectMethod(jniEnv, TestCCallJava, getTime);
- cstr = (char*) (*jniEnv)->GetStringUTFChars(jniEnv,jstr, 0);
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "Success Get Time from Java , Value = %s",cstr );
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "GetTime End" );
- (*jniEnv)->ReleaseStringUTFChars(jniEnv, jstr, cstr);
- (*jniEnv)->DeleteLocalRef(jniEnv, jstr);
- }
- /**
- * SayHello ---- 调用 Java 方法
- */
- void SayHello() {
- if(TestCCallJava == NULL || mTestCCallJava == NULL || sayHello == NULL) {
- int result = InitTestCCallJava() ;
- if(result != 1) {
- return;
- }
- }
- jstring jstrMSG = NULL;
- jstrMSG =(*jniEnv)->NewStringUTF(jniEnv, "Hi,I'm From C");
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "SayHello Begin" );
- (*jniEnv)->CallVoidMethod(jniEnv, mTestCCallJava, sayHello,jstrMSG);
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "SayHello End" );
- (*jniEnv)->DeleteLocalRef(jniEnv, jstrMSG);
- }
关键代码说明:
C中定义映射的类、方法、对象
- jclass TestCCallJava;
- jobject mTestCCallJava;
- jmethodID getTime;
- jmethodID sayHello;
InitTestCCallJava()方法初始化类、方法、对象
初始化类:
- TestCCallJava = (*jniEnv)->FindClass(jniEnv,"com/trunkbow/ccalljava/CCallJava");
初始化对象:
- mTestCCallJava = (*jniEnv)->NewObject(jniEnv, obj_class,construction_id);
静态方法:
- getTime = (*jniEnv)->GetStaticMethodID(jniEnv, TestCCallJava, "getTime","()Ljava/lang/String;");
- sayHello = (*jniEnv)->GetMethodID(jniEnv, TestCCallJava, "sayHello","(Ljava/lang/String;)V");
调用静态方法:
- jstring jstr = NULL;
- char* cstr = NULL;
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "GetTime Begin" );
- jstr = (*jniEnv)->CallStaticObjectMethod(jniEnv, TestCCallJava, getTime);
- cstr = (char*) (*jniEnv)->GetStringUTFChars(jniEnv,jstr, 0);
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "Success Get Time from Java , Value = %s",cstr );
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "GetTime End" );
调用非静态方法
- jstring jstrMSG = NULL;
- jstrMSG =(*jniEnv)->NewStringUTF(jniEnv, "Hi,I'm From C");
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "SayHello Begin" );
- (*jniEnv)->CallVoidMethod(jniEnv, mTestCCallJava, sayHello,jstrMSG);
- __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "SayHello End" );
注意 GetXXXMethodID 和 CallXXXMethod 。
第一个XXX 表示的是映射方法的类型,如: 静态 跟非静态
第二个 XXX 表示 调用方法的返回值 ,如:Void,Object,等等。(调用静态方法的时候Call后面要加Static)
详细 映射方法 和 调用方法 请参考 JNI 文档 ,这个很重要 !
本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/1087657,如需转载请自行联系原作者