android JNI基礎

  前面講到了java和native數據類型,這裏就開始作一下使用:java

       第一步:新建工程ide

       第二部:書寫 java方法:spa

[java] view plaincopyprint?.net

  1. public class NativeMethod {  orm

  2.   

  3.     static {  blog

  4.         System.loadLibrary("com_nedu_jni_jnidemo5-jni");  get

  5.     }  it

  6.     public native boolean getBoolean(boolean b);  io

  7.       

  8.     public native byte getByte(byte b);  class

  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方法,以下:

[cpp] view plaincopyprint?

  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文件

 

[plain] view plaincopyprint?

  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方法

[java] view plaincopyprint?

  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.    }  

 

運行截圖:

相關文章
相關標籤/搜索