Android NDK开发 JNI操作java普通、静态、构造方法
1.Jni实例化一个Java类的实例jobject
1.通过FindClas( ),获取Java类的的jclass
2.通过GetMethodID( ),获取java类的构造方法ID
3.通过NewObject( ),实例化java类并传递参数
2. 通过jobject访问Java类的普通方法
1.获取类名
如果传入的是jobject需要通过GetObjectClass(jobject)获取jclass
如果没有jobject需要通过FindClass("javapackage") 来获类的jclass
2.获取类的方法的jmethodID
3. 通过返回值来调用不通过的方法来调用传入jobject,jmethodID还有参数。 这里注意参数都jint,jstirng,jlong.不要直接传int,long这种类型的
例如
返回值是
无返回值的CallVoidMethod
基本类型的可以直接找到对应的 int 对应CallIntMethod( );boolean对应CallBooleanMethod ( )...
引用类型的方法返回类型是CallObjectMethod。例如String类型调用CallObjectMethod,然后强制转换为jstring
Java层返回值 方法族 本地返回类型NativeType
返回值为void : CallVoidMethod( ) A / V (无)
返回值为引用类型: CallObjectMethod( ) jobect
返回值为boolean : CallBooleanMethod ( ) jboolean
返回值为byte : CallByteMethod( ) jbyte
返回值为char : CallCharMethod( ) jchar
返回值为short CallShortMethod( ) jshort
返回值为int : CallIntMethod( ) jint
返回值为long: CallLongMethod() jlong
返回值为float : CallFloatMethod() jfloat
返回值为double: CallDoubleMethod() jdouble
3. 通过jobject访问Java类的静态方法
1.获取类名
如果传入的是jobject需要通过GetObjectClass(jobject)获取jclass
如果没有jobject需要通过FindClass("javapackage") 来获类的jclass
2.获取类的静态方法的GetStaticMethodID
3.和上面普通一样只是范围时候。都要调用静态的。
例如String 类型的CallStaticObjectMethod
activity
package com.tmf.ndk.type; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView; public class MainActivity extends AppCompatActivity {
public final static String TAG = "MainActivity";
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Student jniNewobj=accessConstructMethod();
Log.i(TAG, "jni new student=" + jniNewobj.toString());
Student student=new Student() ;
String nameAge=getNameAge(student);
Log.i(TAG, "Jni 初始的=" + nameAge);
String nameAge1=setNameAge(student);
Log.i(TAG, "Jni 处理的=" + nameAge1);
Log.i(TAG, "student=" + student.toString());
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(getStaticInfo()); }
//构造方法
public native Student accessConstructMethod();
//JNI 调用对象
public native String getNameAge(Student student);
//JNI 给对象复制
public native String setNameAge(Student student);
//调用静态方法
public native String getStaticInfo(); }
student
package com.tmf.ndk.type; import android.util.Log; public class Student {
public final static String TAG = "Student";
String name;
int age; public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
Log.i(TAG, "getAge=" + age);
return age;
}
public void setAge(int age) {
this.age = age;
Log.i(TAG, "setAge=" + age);
}
public String getName() {
Log.i(TAG, "getName=" + name);
return name;
}
public void setName(String name1) {
this.name = name1;
Log.i(TAG, "setName=" + name);
}
@Override
public String toString() {
String str = "name=" + name + " age=" + age;
return str;
}
public static String getInfo(String name, int age) {
String str = "静态方法 name=" + name + " age=" + age;
Log.i(TAG, "getInfo=" + str);
return str;
}
}
jni
#include <jni.h>
extern "C"{ JNIEXPORT jobject JNICALL
Java_com_tmf_ndk_type_MainActivity_accessConstructMethod(JNIEnv *env, jobject instance);
JNIEXPORT jstring JNICALL
Java_com_tmf_ndk_type_MainActivity_getNameAge(JNIEnv *env, jobject instance, jobject student);
JNIEXPORT jstring JNICALL
Java_com_tmf_ndk_type_MainActivity_setNameAge(JNIEnv *env, jclass type, jobject student);
JNIEXPORT jstring JNICALL
Java_com_tmf_ndk_type_MainActivity_getStaticInfo(JNIEnv *env, jobject instance); } JNIEXPORT jobject JNICALL
Java_com_tmf_ndk_type_MainActivity_accessConstructMethod(JNIEnv *env, jobject instance) {
//获取Student的jclass
jclass studentClass=env->FindClass("com/tmf/ndk/type/Student");
//获取Student的构造方法ID
jmethodID studentCont=env->GetMethodID(studentClass,"<init>","(Ljava/lang/String;I)V");
//实例化Student并传递参数
jstring name = env->NewStringUTF("JIN 构造方法新建对象 宝强");
jint age=;
jobject student= env->NewObject(studentClass,studentCont,name,age);
return student;
} JNIEXPORT jstring JNICALL
Java_com_tmf_ndk_type_MainActivity_getNameAge(JNIEnv *env, jobject instance, jobject student) {
// jclass studentClass=env->FindClass("com/tmf/ndk/type/Student");
//通过实例类获取类名com.tmf.ndk.type.Student
jclass studentClass=env->GetObjectClass(student);
//获取类的getName方法名
jmethodID getName=env->GetMethodID(studentClass,"getName","()Ljava/lang/String;");
//通过实例类调用getName方法名
jstring name=(jstring)env->CallObjectMethod(student,getName); //getAge
jmethodID getAge=env->GetMethodID(studentClass,"getAge","()I");
//通过实例类调用getAge方法名
jint age=env->CallIntMethod(student,getAge); //toString
jmethodID toString=env->GetMethodID(studentClass,"toString","()Ljava/lang/String;");
//通过实例类调用toString方法名
jstring toStr=(jstring)env->CallObjectMethod(student,toString); const char *newStr = env->GetStringUTFChars(toStr, );
return env->NewStringUTF(newStr);
} JNIEXPORT jstring JNICALL
Java_com_tmf_ndk_type_MainActivity_setNameAge(JNIEnv *env, jclass type, jobject student) {
//通过实例类获取类名com.tmf.ndk.type.Student
jclass studentClass=env->GetObjectClass(student);
jmethodID setName=env->GetMethodID(studentClass,"setName","(Ljava/lang/String;)V");
jstring name = env->NewStringUTF("李四");
env->CallVoidMethod(student,setName,name);
//setAge
jmethodID setAge=env->GetMethodID(studentClass,"setAge","(I)V");
jint age=;
//setAge
env->CallVoidMethod(student,setAge,age);
//toString
jmethodID toString=env->GetMethodID(studentClass,"toString","()Ljava/lang/String;");
//通过实例类调用toString方法名
jstring toStr=(jstring)env->CallObjectMethod(student,toString); const char *newStr = env->GetStringUTFChars(toStr, );
return env->NewStringUTF(newStr); } JNIEXPORT jstring JNICALL
Java_com_tmf_ndk_type_MainActivity_getStaticInfo(JNIEnv *env, jobject instance) {
jclass studentClass=env->FindClass("com/tmf/ndk/type/Student");
jmethodID geInfo=env->GetStaticMethodID(studentClass,"getInfo","(Ljava/lang/String;I)Ljava/lang/String;");
jstring name = env->NewStringUTF("静态方法调用 老王");
jint age=;
jstring str=(jstring)env->CallStaticObjectMethod(studentClass,geInfo,name,age);
const char *newStr = env->GetStringUTFChars(str, );
return env->NewStringUTF(newStr);
}