生活随笔
收集整理的這篇文章主要介紹了
Android JNI入门第五篇——基本数据类型使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前面講到了java和native數據類型,這里就開始做一下使用:
?????? 第一步:新建工程
?????? 第二部:書寫 java方法:
[java]?view plaincopyprint?
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方法,如下:
[cpp]?view plaincopyprint?
#include<stdio.h>????#include?<stdlib.h>????#include?"com_nedu_jni_jnidemo5_NativeMethod.h"??????????JNIEXPORT?jboolean?JNICALL?Java_com_nedu_jni_jnidemo5_NativeMethod_getBoolean????(JNIEnv?*e,?jobject?thiz,?jboolean?b){????????????return?b;????}????JNIEXPORT?jbyte?JNICALL?Java_com_nedu_jni_jnidemo5_NativeMethod_getByte????(JNIEnv?*e,?jobject?thiz,?jbyte?by){???????return?by;????}????JNIEXPORT?jchar?JNICALL?Java_com_nedu_jni_jnidemo5_NativeMethod_getChar????(JNIEnv?*e,?jobject?thiz,?jchar?c){?????return?c;?????????}????JNIEXPORT?jshort?JNICALL?Java_com_nedu_jni_jnidemo5_NativeMethod_getShort????(JNIEnv?*e,?jobject?thiz,?jshort?s){???????return?s;????}????JNIEXPORT?jint?JNICALL?Java_com_nedu_jni_jnidemo5_NativeMethod_getInt????(JNIEnv?*e,?jobject?thiz,?jint?i){??????????return?i;????}????JNIEXPORT?jlong?JNICALL?Java_com_nedu_jni_jnidemo5_NativeMethod_getLong????(JNIEnv?*e,?jobject?thiz,?jlong?l){???????????????return?l;????}????JNIEXPORT?jfloat?JNICALL?Java_com_nedu_jni_jnidemo5_NativeMethod_getFloat????(JNIEnv?*e,?jobject?thiz,?jfloat?f){??????????return?f;????}????JNIEXPORT?jdouble?JNICALL?Java_com_nedu_jni_jnidemo5_NativeMethod_getDouble????(JNIEnv?*e,?jobject?thiz,?jdouble?d){??????????????return?d;????}?? ?????? 第五步:制作mk文件
?
[plain]?view plaincopyprint?
LOCAL_PATH?:=?$(call?my-dir)????include?$(CLEAR_VARS)????LOCAL_MODULE????:=?com_nedu_jni_jnidemo5-jni??LOCAL_SRC_FILES?:=NativeMethod.c????include?$(BUILD_SHARED_LIBRARY)??
?
????? 第六步:調用native方法
[java]?view plaincopyprint?
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
*
*/
?
[java]?view plaincopyprint?
???
轉載于:https://www.cnblogs.com/Free-Thinker/p/4500260.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的Android JNI入门第五篇——基本数据类型使用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。