zoukankan      html  css  js  c++  java
  • 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);  
    21. }  

     

               第三部:调用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.   }  

           第五步:制作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)+" "+  
    9.             "返回byte:"+method.getByte((byte) 0)+" "+  
    10.             "返回char:"+method.getChar('c')+" "+  
    11.             "返回short:"+method.getShort((short) 1)+" "+  
    12.             "返回int:"+method.getInt(1)+" "+  
    13.             "返回long:"+method.getLong(9)+" "+  
    14.             "返回float:"+method.getFloat((float) 1.0)+" "+  
    15.             "返回double:"+method.getDouble(2.0)+" ");  
    16.    }  

     

    运行截图:


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

    /**
    * @author 张兴业
    * 邮箱:xy-zhang#163.com
    * android开发进阶群:278401545
    *
    */

      1.    
  • 相关阅读:
    Map根据key或者value排序
    docker部署Javaweb项目(jdk+tomcat+mysql)
    MySQL设置某一字段默认为0,但是插入数据化却显示为null
    文件下载
    JXLS导出Excel(模板导出)
    eclipse使用lombok
    Integer 类型数值判断相等的坑
    通用Mapper相关
    SSM配置Socket多线程编程(RFID签到实例)
    使用JSONObject遇到的问题,java.lang.NoClassDefFoundError: net/sf/json/JSONObject
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4500260.html
Copyright © 2011-2022 走看看