Android JNI入门第六篇——基本数据类型使用
2017-02-23 15:07
411 查看
转载请标明出处:
http://blog.csdn.net/michael1112/article/details/56672288
江东橘子的博客前面讲到了Java和native数据类型,这里就开始做一下使用:
第一步:新建工程
第二步:书写 java方法:
public class NativeMethod { static { System.loadLibrary("com_nedu_jni_jnidemo5-jni"); } public native boolean getBoolean(boolean b); public native byte getByte(byte b); public native char getChar(char c); public native short getShort(short s); public native int getInt(int i); public native long getLong(long l); public native float getFloat(float f); public native double getDouble(double d); }
第三步:调用javah命令生成h文件。
第四步:补充native方法,如下
#include<stdio.h> #include <stdlib.h> #include "com_nedu_jni_jnidemo5_NativeMethod.h" /* * Class: com_nedu_jni_jnidemo5_NativeMethod * Method: getBoolean * Signature: (Z)Z */ JNIEXPORT jboolean JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getBoolean (JNIEnv *e, jobject thiz, jboolean b){ return b; } /* * Class: com_nedu_jni_jnidemo5_NativeMethod * Method: getByte * Signature: (B)B */ JNIEXPORT jbyte JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getByte (JNIEnv *e, jobject thiz, jbyte by){ return by; } /* * Class: com_nedu_jni_jnidemo5_NativeMethod * Method: getChar * Signature: (C)C */ JNIEXPORT jchar JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getChar (JNIEnv *e, jobject thiz, jchar c){ return c; } /* * Class: com_nedu_jni_jnidemo5_NativeMethod * Method: getShort * Signature: (S)S */ JNIEXPORT jshort JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getShort (JNIEnv *e, jobject thiz, jshort s){ return s; } /* * Class: com_nedu_jni_jnidemo5_NativeMethod * Method: getInt * Signature: (I)I */ JNIEXPORT jint JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getInt (JNIEnv *e, jobject thiz, jint i){ return i; } /* * Class: com_nedu_jni_jnidemo5_NativeMethod * Method: getLong * Signature: (J)J */ JNIEXPORT jlong JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getLong (JNIEnv *e, jobject thiz, jlong l){ return l; } /* * Class: com_nedu_jni_jnidemo5_NativeMethod * Method: getFloat * Signature: (F)F */ JNIEXPORT jfloat JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getFloat (JNIEnv *e, jobject thiz, jfloat f){ return f; } /* * Class: com_nedu_jni_jnidemo5_NativeMethod * Method: getDouble * Signature: (D)D */ JNIEXPORT jdouble JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getDouble (JNIEnv *e, jobject thiz, jdouble d){ return d; }
第五步 调用native 方法
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView text=(TextView)findViewById(R.id.text); NativeMethod method=new NativeMethod(); text.setText("返回boolean:"+method.getBoolean(true)+"\n"+ "返回byte:"+method.getByte((byte) 0)+"\n"+ "返回char:"+method.getChar('c')+"\n"+ "返回short:"+method.getShort((short) 1)+"\n"+ "返回int:"+method.getInt(1)+"\n"+ "返回long:"+method.getLong(9)+"\n"+ "返回float:"+method.getFloat((float) 1.0)+"\n"+ "返回double:"+method.getDouble(2.0)+"\n"); }
运行结果:
总结:JNI中传过来的java基本类型可以直接使用。
/**
* @author 江东橘子
* 邮箱:whtchl@126.com
*/
相关文章推荐
- Android JNI入门第五篇——基本数据类型使用
- Golang入门教程(七)基本数据类型使用案例
- JAVA入门.零基础学Java语言 MOOC 第6周 使用对象(基本数据类型的包裹类型对象) 第7周 函数
- Android JNI入门第五篇——基本数据类型使用
- Android JNI入门第五篇——基本数据类型使用
- Android JNI入门第五篇——基本数据类型使用
- JavaScript: * JavaScript:基本使用: * ECMAScript: * 变量:弱变量类型: var i; * 数据类型:原始类型和引用类型.
- python 入门-输入输出与基本数据类型
- java----基本数据类型精度的研究与使用(char,byte,int等)
- go语言快速入门:数据类型使用例(5)
- python 入门学习笔记之基本语法与基本数据类型
- VB使用的基本数据类型
- Redis的数据类型基本使用,以及简…
- 【VB超简单入门】六、基本数据类型
- 今日内容介绍 1、自定义类型的定义及使用 2、自定义类的内存图 3、ArrayList集合的基本功能 4、随机点名器案例及库存案例代码优化 ###01引用数据类型_类 * A: 数据类型
- 基本数据类型与表达式2 - 零基础入门学习Delphi03
- 基本数据类型与表达式3 - 零基础入门学习Delphi04
- scala 开发入门(1)-- 变量及基本数据类型
- scala 开发入门(1)-- 变量及基本数据类型
- 基本数据类型的使用