博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Studio NDK:二、JNI 返回JAVA 实体
阅读量:6413 次
发布时间:2019-06-23

本文共 2302 字,大约阅读时间需要 7 分钟。

本篇是承接的基础上进行的 #####1.编写Books实体类 /** * @author jayqiu * @describe * @date 2017/3/30 17:21 / public class Books { private String title; private String author; private String subject; private int book_id; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public int getBook_id() { return book_id; } public void setBook_id(int book_id) { this.book_id = book_id; } } #####2.编写MyNdk.java /* * @author jayqiu * @describe * @date 2017/3/30 13:52 */ public class MyNdk { static { System.loadLibrary("MyNdk"); } public native int getAdd(int a,int b); public native Books getBooks(Books books); }

#####3.MyNdk.c生成Java_com_threehalf_mynewjni_MyNdk_getBooks 方法 JNIEXPORT jobject JNICALL Java_com_threehalf_mynewjni_MyNdk_getBooks(JNIEnv *env, jobject instance, jobject books) { jclass objectClass = (*env)->FindClass(env, "com/threehalf/mynewjni/Books");// java 实体类对应的位置 jfieldID title = (*env)->GetFieldID(env,objectClass, "title", "Ljava/lang/String;");//book title jfieldID book_id =(*env)->GetFieldID(env,objectClass, "book_id", "I");//book id

(*env)->SetObjectField(env,books, title, (*env)->NewStringUTF(env,"C 语言"));//设置booktitle    (*env)->SetIntField(env,books, book_id, 10);// 设置 book id    return  books;}复制代码

#####4.MainActivity public class MainActivity extends AppCompatActivity {

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TextView textView = (TextView) findViewById(R.id.tv_name);        // textView.setText(new MyNdk().getAdd(10,20)+"");        Books books = new Books();        books = (Books) new MyNdk().getBooks(books);        textView.setText("ID:"+books.getBook_id() + "书名:" + books.getTitle());    }}复制代码

#####5.运行结果

#####6.GetFieldID Java基础类型对应的符号

Java类型 符号
boolean Z
byte B
char C
short S
int I
long L
float F
double D
void V
objects对象 Lfully-qualified-class-name; L类名;
Arrays数组 [array-type [数组类型
methods方法 (argument-types)return-type(参数类型)返回类型

转载于:https://juejin.im/post/5b873e2951882542b13d3738

你可能感兴趣的文章
spring事务管理
查看>>
【腾讯开源】iOS爆内存问题解决方案-OOMDetector组件
查看>>
Linux TTY、PTS、PTY详解
查看>>
java泛型中T、E、K、V、?等含义
查看>>
UITableView中使用reloadRowsAtIndexPaths会出现闪退的解决办法
查看>>
Banner无限轮播图
查看>>
Java 静态代理、Java动态代理、CGLIB动态代理
查看>>
zabbix监控memcached模板
查看>>
JavaScript中的对象
查看>>
asp判断接受的参数是否为纯数字
查看>>
Lua中的table函数库
查看>>
阿斯顿发生点
查看>>
Android 图片倒影效果源码
查看>>
HADOOP2.0,Exception java.lang.NoClassDefFoundError: org/apache/hadoop/mapreduce/v2/app/MRAppMaster
查看>>
Planning, Deploying, and Monitoring Mobility
查看>>
Win7 + VS2010 + Python2.7.5 安装 gevent
查看>>
pd 数据类型对照表
查看>>
@Transactional
查看>>
phpdoc
查看>>
webkit内核浏览器的CSS写法
查看>>