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,如需转载请自行联系原作者



相关文章
|
1天前
|
Android开发
Android JNI与CAN通信遇到的问题总结
Android JNI与CAN通信遇到的问题总结
14 1
|
2天前
|
Android开发
Android JNI 报错(signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr )
Android JNI 报错(signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr )
18 1
|
8天前
|
Android开发 开发者
Android网络和数据交互: 请解释Android中的AsyncTask的作用。
Android&#39;s AsyncTask simplifies asynchronous tasks for brief background work, bridging UI and worker threads. It involves execute() for starting tasks, doInBackground() for background execution, publishProgress() for progress updates, and onPostExecute() for returning results to the main thread.
9 0
|
8天前
|
网络协议 安全 API
Android网络和数据交互: 什么是HTTP和HTTPS?在Android中如何进行网络请求?
HTTP和HTTPS是网络数据传输协议,HTTP基于TCP/IP,简单快速,HTTPS则是加密的HTTP,确保数据安全。在Android中,过去常用HttpURLConnection和HttpClient,但HttpClient自Android 6.0起被移除。现在推荐使用支持TLS、流式上传下载、超时配置等特性的HttpsURLConnection进行网络请求。
9 0
|
22天前
|
XML Java Android开发
Android每点击一次按钮就添加一条数据
Android每点击一次按钮就添加一条数据
23 1
|
1月前
|
Android开发
[Android jni] Bitmap与Mat对象的相互转换
[Android jni] Bitmap与Mat对象的相互转换
50 0
|
1月前
|
测试技术 API 调度
【Android 从入门到出门】第七章:开始使用WorkManager
【Android 从入门到出门】第七章:开始使用WorkManager
20 3
【Android 从入门到出门】第七章:开始使用WorkManager
|
1月前
|
存储 Android开发 C++
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
34 3
|
1月前
|
Android开发
【Android 从入门到出门】第四章:现代Android开发中的导航
【Android 从入门到出门】第四章:现代Android开发中的导航
22 2
【Android 从入门到出门】第四章:现代Android开发中的导航
|
1月前
|
XML API Android开发
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
26 4