Android JNI入门第五篇——基本数据类型使用

简介:

前面讲到了java和native数据类型,这里就开始做一下使用:

第一步:新建工程

第二部:书写 java方法:

 
  1. public class NativeMethod {  
  2.  
  3.     static {  
  4.         System.loadLibrary("com_nedu_jni_jnidemo5-jni");  
  5.     }  
  6.     public native boolean getBoolean(boolean b);  
  7.       
  8.     public native byte getByte(byte b);  
  9.       
  10.     public native char getChar(char c);  
  11.       
  12.     public native short getShort(short s);  
  13.       
  14.     public native int getInt(int i);  
  15.       
  16.     public native long getLong(long l);  
  17.       
  18.     public native float getFloat(float f);  
  19.       
  20.     public native double getDouble(double d);  

第三部:调用javac、javah命令生成h文件。

第四部:补充native方法,如下:

 
  1. #include<stdio.h>    
  2. #include <stdlib.h>    
  3. #include "com_nedu_jni_jnidemo5_NativeMethod.h"    
  4.  
  5.  
  6.  
  7. /*  
  8.  * Class:     com_nedu_jni_jnidemo5_NativeMethod  
  9.  * Method:    getBoolean  
  10.  * Signature: (Z)Z  
  11.  */ 
  12. JNIEXPORT jboolean JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getBoolean  
  13.   (JNIEnv *e, jobject thiz, jboolean b){  
  14.     
  15.       return b;  
  16.   }  
  17.  
  18. /*  
  19.  * Class:     com_nedu_jni_jnidemo5_NativeMethod  
  20.  * Method:    getByte  
  21.  * Signature: (B)B  
  22.  */ 
  23. JNIEXPORT jbyte JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getByte  
  24.   (JNIEnv *e, jobject thiz, jbyte by){  
  25.      return by;  
  26.   }  
  27.  
  28. /*  
  29.  * Class:     com_nedu_jni_jnidemo5_NativeMethod  
  30.  * Method:    getChar  
  31.  * Signature: (C)C  
  32.  */ 
  33. JNIEXPORT jchar JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getChar  
  34.   (JNIEnv *e, jobject thiz, jchar c){  
  35.    return c;  
  36.      
  37.   }  
  38.  
  39. /*  
  40.  * Class:     com_nedu_jni_jnidemo5_NativeMethod  
  41.  * Method:    getShort  
  42.  * Signature: (S)S  
  43.  */ 
  44. JNIEXPORT jshort JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getShort  
  45.   (JNIEnv *e, jobject thiz, jshort s){  
  46.      return s;  
  47.   }  
  48.  
  49. /*  
  50.  * Class:     com_nedu_jni_jnidemo5_NativeMethod  
  51.  * Method:    getInt  
  52.  * Signature: (I)I  
  53.  */ 
  54. JNIEXPORT jint JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getInt  
  55.   (JNIEnv *e, jobject thiz, jint i){  
  56.         return i;  
  57.   }  
  58.  
  59. /*  
  60.  * Class:     com_nedu_jni_jnidemo5_NativeMethod  
  61.  * Method:    getLong  
  62.  * Signature: (J)J  
  63.  */ 
  64. JNIEXPORT jlong JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getLong  
  65.   (JNIEnv *e, jobject thiz, jlong l){  
  66.     
  67.          return l;  
  68.   }  
  69.  
  70. /*  
  71.  * Class:     com_nedu_jni_jnidemo5_NativeMethod  
  72.  * Method:    getFloat  
  73.  * Signature: (F)F  
  74.  */ 
  75. JNIEXPORT jfloat JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getFloat  
  76.   (JNIEnv *e, jobject thiz, jfloat f){  
  77.         return f;  
  78.   }  
  79.  
  80. /*  
  81.  * Class:     com_nedu_jni_jnidemo5_NativeMethod  
  82.  * Method:    getDouble  
  83.  * Signature: (D)D  
  84.  */ 
  85. JNIEXPORT jdouble JNICALL Java_com_nedu_jni_jnidemo5_NativeMethod_getDouble  
  86.   (JNIEnv *e, jobject thiz, jdouble d){  
  87.     
  88.         return d;  
  89.   }  
  90.  
  91.  
  92.  

第五步:制作mk文件

 
  1. LOCAL_PATH := $(call my-dir)  
  2.  
  3. include $(CLEAR_VARS)  
  4.  
  5. LOCAL_MODULE    := com_nedu_jni_jnidemo5-jni  
  6. LOCAL_SRC_FILES :=NativeMethod.c  
  7.  
  8. include $(BUILD_SHARED_LIBRARY)  

 第六步:调用native方法

 
  1. public void onCreate(Bundle savedInstanceState) {  
  2.        super.onCreate(savedInstanceState);  
  3.        setContentView(R.layout.main);  
  4.          
  5.        TextView text=(TextView)findViewById(R.id.text);  
  6.        NativeMethod method=new NativeMethod();  
  7.          
  8.        text.setText("返回boolean:"+method.getBoolean(true)+"\n"+  
  9.             "返回byte:"+method.getByte((byte0)+"\n"+  
  10.             "返回char:"+method.getChar('c')+"\n"+  
  11.             "返回short:"+method.getShort((short1)+"\n"+  
  12.             "返回int:"+method.getInt(1)+"\n"+  
  13.             "返回long:"+method.getLong(9)+"\n"+  
  14.             "返回float:"+method.getFloat((float1.0)+"\n"+  
  15.             "返回double:"+method.getDouble(2.0)+"\n");  
  16.    } 

运行截图:

总结:JNI中传过来的java基本类型可以直接使用。
 



     本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/817170,如需转载请自行联系原作者



相关文章
|
18天前
|
Android开发
Android JNI与CAN通信遇到的问题总结
Android JNI与CAN通信遇到的问题总结
37 1
|
18天前
|
消息中间件 网络协议 Java
Android 开发中实现数据传递:广播和Handler
Android 开发中实现数据传递:广播和Handler
16 1
|
19天前
|
Android开发
Android JNI 报错(signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr )
Android JNI 报错(signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr )
60 1
|
4天前
|
Java Linux API
统计android设备的网络数据使用量
统计android设备的网络数据使用量
14 0
|
1天前
|
Android开发 数据库管理
Android如何在Activity和Service之间传递数据
Android如何在Activity和Service之间传递数据
|
2天前
|
XML JSON API
转Android上基于JSON的数据交互应用
转Android上基于JSON的数据交互应用
|
2天前
|
Android开发
Android游戏引擎AndEngine入门资料
Android游戏引擎AndEngine入门资料
|
2天前
|
JSON Android开发 数据格式
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
11 2
|
2天前
|
Java Android开发
android AsyncTask入门
android AsyncTask入门
|
3天前
|
Java API 开发工具
java与Android开发入门指南
java与Android开发入门指南
10 0