美文网首页
c/c++调用java属性和函数

c/c++调用java属性和函数

作者: simple_0955 | 来源:发表于2018-01-23 20:57 被阅读0次

NIEXPORT jstring JNICALL

Java_com_example_liyang_myapplication_MainActivity_stringFromJNI(

JNIEnv *env,

jobject jobj) {

int i=0;

//非静态属性

    jclass clazz=env->GetObjectClass(jobj);

jfieldID jintid=env->GetFieldID(clazz,"anInt","I");

int a=25;

LOGE("%d",a);

env->SetIntField(jobj, jintid, a);

//静态属性

    jfieldID js_intid=env->GetStaticFieldID(clazz,"s_anInt","I");

LOGE("%d %d",i++,env->GetStaticIntField(clazz,js_intid));

env->SetStaticIntField(clazz, js_intid,35);

LOGE("%d %d",i++,env->GetStaticIntField(clazz,js_intid));

//方法

    jmethodID jsmid=env->GetMethodID(clazz,"temp","(I)I");

env->CallIntMethod(jobj, jsmid,56);

//静态方法

//    stemp

    jmethodID jsmi=env->GetStaticMethodID(clazz,"stemp","(ILjava/lang/String;)I");

jstring str=env->NewStringUTF("hello");

env->CallStaticIntMethod(clazz,jsmi,11,str);

//子类构造方法

    jfieldID jsid=env->GetFieldID(clazz,"father","Lcom/example/liyang/myapplication/Father;");

jobject son1=env->GetObjectField(jobj, jsid);

jclass cls_son=env->GetObjectClass(son1);

jmethodID jomid=env->GetMethodID(cls_son,"drive","()V");

env->CallVoidMethod(son1, jomid,NULL);

//父类构造方法

    jclass  jfcls=env->GetSuperclass(cls_son);

jmethodID jfomid=env->GetMethodID(jfcls,"drive","()V");

env->CallNonvirtualVoidMethod(son1, jfcls, jfomid,NULL);

std::string hello ="Hello from C++";

return env->NewStringUTF(hello.c_str());

}

相关文章

网友评论

      本文标题:c/c++调用java属性和函数

      本文链接:https://www.haomeiwen.com/subject/mzojaxtx.html