Android------- jni返回结构体
1.创建工程,添加jni
文章图片
2. main.cpp
#include struct test_data
{
char name[256];
intnum;
double average;
};
extern "C" {
JNIEXPORT jobjectJNICALL Java_com_re_struct_MainActivity_call(JNIEnv * env, jobject obj, jobject classobj);
};
JNIEXPORT jobjectJNICALL Java_com_re_struct_MainActivity_call(JNIEnv * env, jobject obj, jobject classobj)
{
jclass objectClass = env->FindClass("com/re/struct/test_data");
jfieldID name = env->GetFieldID(objectClass, "name", "Ljava/lang/String;
");
jfieldID num = env->GetFieldID(objectClass, "num", "I");
jfieldID average = env->GetFieldID(objectClass, "average", "D");
env->SetObjectField(classobj, name, env->NewStringUTF("hello world"));
env->SetIntField(classobj, num, 10);
env->SetDoubleField(classobj, average, 2.5);
return classobj;
}
3. test_data.java
【Android------- jni返回结构体】
package com.re.struct;
public class test_data {
public String name;
public intnum;
public double average;
}
4. MainActivity.java
package com.re.struct;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
static{
System.loadLibrary("main");
} private native test_data call(test_data data);
private test_data data = https://www.it610.com/article/new test_data();
private static final String TAG="######";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data = https://www.it610.com/article/call(data);
Log.e(TAG,"name:" + data.name);
Log.e(TAG, "num:" + data.num);
Log.e(TAG, "average:" + data.average);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
5. 效果:
文章图片
6. 补充:
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(参数类型)返回类型 |
推荐阅读
- 前端代码|前端代码 返回顶部 backToTop
- Android|Android JNI之静态注册(android studio)
- 后端返回image的Buffer,显示图片
- 使用sqlalchemy|使用sqlalchemy orm 的model序列化,解决返回model的异常
- 【数组题】给定一个二进制矩阵|【数组题】给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果。
- RF接口返回数据验证举例
- 三行代码禁用侧滑返回
- SpringMVC返回的ResponseEntity出现乱码及解决
- Android|Android JNI 篇 - 编译 bilibili/ijkPlayer
- 和春天有个约会(六)