大家好,又见面了,我是全栈君。
NewObject/NewString/New<TYPE>Array :new新对象
Get/Set<TYPE>Field:获取属性
Get/SetStatic<TYPE>Field :获取静态属性
Call<TYPE>Method/CallStatic<TYPE>Method:调用方法
Java类型 别名 C++本地类型 字节(bit)
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
3
2
double
jdouble
double
64
void
void
n/a
Object _jobject *jobject
TestProvider
,该类有2个方法分别为
String getTime( )
,
void saysayHello( String str)
jobject mTestProvider;
jmethodID getTime;
jmethodID sayHello;
“
com/duicky/TestProvider
“);
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);
Get
XXX
MethodID 和
Call
XXX
Method 。
clazz代表该属性所在的类,name表示方法名称,sign是签名
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类型签名···)返回值类型签名
签名
f1() ()V
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
图解签名:
javap -s -p [full class Name]
private video1.TestNative
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++代码
“
video1_TestNative.h
“
#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
本文转自http://www.cnblogs.com/likwo/archive/2012/05/21/2512400.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/121338.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...