首页 > 代码库 > Android JNI入门第六篇——C调用Java
Android JNI入门第六篇——C调用Java
本篇将介绍在JNI编程中C调用Java实现。
源码下载地址:http://download.csdn.net/detail/xyz_lmn/4868265
关键代码:
java:
[java] view plaincopyprint?
- 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:
[cpp] view plaincopyprint?
- #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);
- }
关键代码说明:
[cpp] view plaincopyprint?
- jclass TestCCallJava;
- jobject mTestCCallJava;
- jmethodID getTime;
- jmethodID sayHello;
InitTestCCallJava()方法初始化类、方法、对象
初始化类:
[html] view plaincopyprint?
- TestCCallJava = (*jniEnv)->FindClass(jniEnv,"com/trunkbow/ccalljava/CCallJava");
初始化对象:
[html] view plaincopyprint?
- mTestCCallJava = (*jniEnv)->NewObject(jniEnv, obj_class,construction_id);
静态方法:
[html] view plaincopyprint?
- getTime = (*jniEnv)->GetStaticMethodID(jniEnv, TestCCallJava, "getTime","()Ljava/lang/String;");
[html] view plaincopyprint?
- sayHello = (*jniEnv)->GetMethodID(jniEnv, TestCCallJava, "sayHello","(Ljava/lang/String;)V");
调用静态方法:
[html] view plaincopyprint?
- 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" );
调用非静态方法
[html] view plaincopyprint?
- 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 文档 ,这个很重要 !
工程的其他代码可在http://download.csdn.net/detail/xyz_lmn/4868265下载
参考:
http://game.ceeger.com/Script/AndroidJNI/AndroidJNI.html
/**
* @author 张兴业
* http://blog.csdn.net/xyz_lmn
* iOS入门群:83702688
* android开发进阶群:241395671
* 我的新浪微博:@张兴业TBOW
*/
Android JNI入门第六篇——C调用Java
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。