您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本案例中编写 android app中对应的jni函数
static native Student getStudent();
jni中获取自定义的类函数描述定向总结
定义一个char *指向自定义类
![[android底层]jni中获取自定义的类函数编写](https://note.wiz.cn/unzip/232b0826-d65c-4c92-bde7-64832286df9e/7e662a17-53db-47cd-811e-da3620355352.1259/index_files/892e1aba-c1f4-4bd8-8fc1-9ee498f3b732.png)
1、返回类型:jobject
![[android底层]jni中获取自定义的类函数编写](https://note.wiz.cn/unzip/232b0826-d65c-4c92-bde7-64832286df9e/7e662a17-53db-47cd-811e-da3620355352.1259/index_files/0ad0acd9-d8c1-427c-bfd7-b42f455dc2b0.png)
2、获取自定义的类
![[android底层]jni中获取自定义的类函数编写](https://note.wiz.cn/unzip/232b0826-d65c-4c92-bde7-64832286df9e/7e662a17-53db-47cd-811e-da3620355352.1259/index_files/722ebe3a-1439-42b9-a90f-c93eedf147ae.png)
3、通过自定义类的构造函数在jni中构建jobject类:获取方法的域id的描述符填写"<init>"
![[android底层]jni中获取自定义的类函数编写](https://note.wiz.cn/unzip/232b0826-d65c-4c92-bde7-64832286df9e/7e662a17-53db-47cd-811e-da3620355352.1259/index_files/1cdb2615-eefc-4033-9d18-3b09a580bbc6.png)
4、jni回调app中的特定方法来设置自定义对象
![[android底层]jni中获取自定义的类函数编写](https://note.wiz.cn/unzip/232b0826-d65c-4c92-bde7-64832286df9e/7e662a17-53db-47cd-811e-da3620355352.1259/index_files/260ceaff-a1e1-4281-a833-0257049c89e4.png)
或者不使用回调来设置对象属性;set***Field
![[android底层]jni中获取自定义的类函数编写](https://note.wiz.cn/unzip/232b0826-d65c-4c92-bde7-64832286df9e/7e662a17-53db-47cd-811e-da3620355352.1259/index_files/c1df66c6-213b-473c-aed5-5061d685b2d4.png)
5、最后返回jobject对象
![[android底层]jni中获取自定义的类函数编写](https://note.wiz.cn/unzip/232b0826-d65c-4c92-bde7-64832286df9e/7e662a17-53db-47cd-811e-da3620355352.1259/index_files/a5683eca-109c-4d05-97b2-7d32d459dd07.png)
完整代码
const char * jnistudentname = "com/jniacpp/Student";
jobject getStudent(JNIEnv *env, jobject thiz){
jclass stuClass = env->FindClass(jnistudentname);
//取得构造方法的域id
jmethodID constuctId = env->GetMethodID(stuClass, "<init>", "()V");
// 构建obj对象
jobject stuObj = env->NewObject(stuClass, constuctId);
//取得特定方法的域id
jmethodID setStudentId = env->GetMethodID(stuClass, "setStudent", "(ILjava/lang/String;Z)V");
//调用app中的setStuudent函数
env->CallVoidMethod(stuObj, setStudentId, "110917010", "mrzhang", true);
/*
//取得各个属性的域id;
jfieldID nameID = env->GetFieldID(stuClass, "name", "Ljava/lang/String;");
jfieldID idID = env->GetFieldID(stuClass, "id", "I");
jfieldID isBoyID = env->GetFieldID(stuClass, "isBoy", "Z");
env->SetIntField(stuObj, nameID, 21);
env->SetObjectField(stuObj, nameID, env->NewStringUTF("zhang sir"));
env->SetIntField(stuObj, isBoyID, true);
*/
return stuObj;
}参考电子书
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。