boolean jboolean unsigned char 8, unsigned
byte jbyte signed char 8
char jchar unsigned short 16, unsigned
short jshort short 16
int jint long 32
long jlong __int64 64
float jfloat float 32
double jdouble double 64
void void n/a
Object _jobject *jobject
jobject mTestProvider;
jmethodID getTime;
jmethodID sayHello;
C中新建对象
jmethodID construction_id = (*jniEnv)->GetMethodID(jniEnv, TestProvider,"<init>", "()V");
//通过NewObject来创建对象
jobject mTestProvider = (*jniEnv)->NewObject(jniEnv, TestProvider,construction_id);
getTime = (*jniEnv)->GetStaticMethodID(jniEnv, TestProvider, "getTime","()Ljava/lang/String;");
非静态:
sayHello = (*jniEnv)->GetMethodID(jniEnv, TestProvider, "sayHello","(Ljava/lang/String;)V");
(*jniEnv)->CallStaticObjectMethod(jniEnv, TestProvider, getTime);
非静态:
(*jniEnv)->CallVoidMethod(jniEnv, mTestProvider, sayHello,jstrMSG);
boolean Z
byte B
char C
short S
int I
long J
float F
double D
void V
object L用/分隔包的完整类名: Ljava/lang/String;
Array [签名 [I [Ljava/lang/Object;
Method (参数1类型签名 参数2类型签名···)返回值类型签名
int f2(int, long) (IJ)I
boolean f3(int[]) ([I)B
double f4(String, int) (Ljava/lang/String;I)D
void f5(int, String [], char) (I[Ljava/lang/String;C)V
图解签名:
Compiled from "TestNative.java"
public class video1.TestNative extends java.lang.Object{
public java.lang.String name;
Signature: Ljava/lang/String;
public video1.TestNative();
Signature: ()V
public int signTest(int, java.util.Date, int[]);
Signature: (ILjava/util/Date;[I)I
public native void sayHello();
Signature: ()V
public static void main(java.lang.String[]);
Signature: ([Ljava/lang/String;)V
}
TestNative完整代码:
import java.util.Date;
public class TestNative {
public String name="Test";
public int number =100;
public int signTest(int i,Date date,int[] arr){
System.out.println("Sign Test");
return 0;
}
//native关键字修饰的方法,其内容是C/C++编写的,java中不必为它编写具体的实现
public native void sayHello();
public static void main(String[] args) {
System.loadLibrary("NativeCode");
TestNative tn = new TestNative();
tn.sayHello();
}
}
C/C++代码
#include <iostream>
using namespace std;
JNIEXPORT void JNICALL Java_video1_TestNative_sayHello(JNIEnv * env, jobject obj){
cout<<"Hello Native Test !"<<endl;
//因为test不是静态函数,所以传进来的就是调用这个函数的对象
//否则就传入一个jclass对象表示native()方法所在的类
jclass native_clazz = env->GetObjectClass(obj);
//得到jfieldID
jfieldID fieldID_prop = env->GetFieldID(native_clazz,"name","Ljava/lang/String;");
jfieldID fieldID_num = env->GetFieldID(native_clazz,"number","I");
//得到jmethodID
jmethodID methodID_func=env->GetMethodID(native_clazz,"signTest","(ILjava/util/Date;[I)I");
//调用signTest方法
env->CallIntMethod(obj,methodID_func,1L,NULL,NULL);
//得到name属性
jobject name = env->GetObjectField(obj,fieldID_name);
//得到number属性
jint number= env->GetIntField(obj,fieldID_num);
cout<<number<<endl;//100
//修改number属性的值
env->SetIntField(obj,fieldID_num,18880L);
number= env->GetIntField(obj,fieldID_num);
cout<<number<<endl;//18880
}
本文地址,转载请注明出处:
http://www.cnblogs.com/likwo/archive/2012/05/21/2512400.html
其他推荐学习网站
http://blog.csdn.net/ostrichmyself/article/details/4557851
http://blog.csdn.net/popop123/article/details/1511180
https://www.ibm.com/developerworks/cn/java/j-jni/
JNI 攻略系列
http://blog.csdn.net/yjkwf/article/details/7006260
http://blog.csdn.net/yjkwf/article/details/7006261
http://blog.csdn.net/yjkwf/article/details/7006264
http://blog.csdn.net/yjkwf/article/details/7006266
http://disanji.net/2011/01/26/android-jni-programming-2/
JNI Examples for Android
http://android.wooyd.org/JNIExample/files/JNIExample.pdf
JNI pthread 多线程使用
http://www.cnblogs.com/lknlfy/archive/2012/03/16/2400786.html