This article describes the C call Java implementation in JNI programming.
Source: http://download.csdn.net/detail/xyz_lmn/4868265
Key code:
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);
- /**
- * Initialize classes, objects, methods
- */
- 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;
- }
- /**
- * Get time----call Java method
- */
- 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----Calling Java methods
- */
- 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);
- }
Key code Description:
classes, methods, objects that define mappings in C
[CPP]View Plaincopyprint?
- Jclass Testccalljava;
- Jobject Mtestccalljava;
- Jmethodid GetTime;
- Jmethodid SayHello;
The Inittestccalljava () method initializes the class, method, ObjectInitialize class:
[HTML]View Plaincopyprint?
- Testccalljava = (*jnienv)->findclass (jnienv, "Com/trunkbow/ccalljava/ccalljava");
Initialize the object:
[HTML]View Plaincopyprint?
- Mtestccalljava = (*jnienv)->newobject (jnienv, obj_class,construction_id);
Initialization method: Static method:
[HTML]View Plaincopyprint?
- GetTime = (*jnienv)->getstaticmethodid (jnienv, Testccalljava, "GetTime", "() ljava/lang/string;");
Non-static methods:
[HTML]View Plaincopyprint?
- SayHello = (*jnienv)->getmethodid (jnienv, Testccalljava, "SayHello", "(ljava/lang/string;) V");
methods for calling Java in C
Call a static method:
[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");
Calling a non-static method
[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");
Pay attention to Getxxxmethodid and Callxxxmethod.
The first XXX represents the type of mapping method, such as: Static and non-static
The second XXX represents the return value of the calling method, such as: Void,object, and so on. (Call the static method to add static after calling)
Refer to the JNI documentation for detailed mapping methods and calling methods, which is important!
other code for the project can be downloaded in http://download.csdn.net/detail/xyz_lmn/4868265
Reference:
http://game.ceeger.com/Script/AndroidJNI/AndroidJNI.html
/*** @author Zhang Xingye * http://blog.csdn.net/xyz_lmn* iOS starter Group: 83702688* Android Development Advanced Group: 241395671* my Sina Weibo:
@ Zhang Xingye tbow*/
Android JNI Primer Sixth--c calling Java