Android JNI入門第五篇——基礎資料型別 (Elementary Data Type)使用

來源:互聯網
上載者:User

         前面講到了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);}

 

           第三部:調用javac、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;  }

       第五步:製作mk檔案

 

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := com_nedu_jni_jnidemo5-jniLOCAL_SRC_FILES :=NativeMethod.cinclude $(BUILD_SHARED_LIBRARY)

 

      第六步:調用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 張興業
* 郵箱:xy-zhang#163.com
* android開發進階群:278401545
*
*/

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.