Android JNI入门第五篇——基本数据类型使用
前面讲到了java和native数据类型,这里就开始做一下使用: 第一步:新建工程 第二部:书写 java方法: publicclassNativeMethod{ static{ System.loadLibrary("com_nedu_jni_jnidemo5-jni"); } publicnativebooleangetBoolean(booleanb); publicnativebytegetByte(byteb); publicnativechargetChar(charc); publicnativeshortgetShort(shorts); publicnativeintgetInt(inti); publicnativelonggetLong(longl); publicnativefloatgetFloat(floatf); publicnativedoublegetDouble(doubled); } 第三部:调用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 */ JNIEXPORTjbooleanJNICALLJava_com_nedu_jni_jnidemo5_NativeMethod_getBoolean (JNIEnv*e,jobjectthiz,jbooleanb){ returnb; } /* *Class:com_nedu_jni_jnidemo5_NativeMethod *Method:getByte *Signature:(B)B */ JNIEXPORTjbyteJNICALLJava_com_nedu_jni_jnidemo5_NativeMethod_getByte (JNIEnv*e,jobjectthiz,jbyteby){ returnby; } /* *Class:com_nedu_jni_jnidemo5_NativeMethod *Method:getChar *Signature:(C)C */ JNIEXPORTjcharJNICALLJava_com_nedu_jni_jnidemo5_NativeMethod_getChar (JNIEnv*e,jobjectthiz,jcharc){ returnc; } /* *Class:com_nedu_jni_jnidemo5_NativeMethod *Method:getShort *Signature:(S)S */ JNIEXPORTjshortJNICALLJava_com_nedu_jni_jnidemo5_NativeMethod_getShort (JNIEnv*e,jobjectthiz,jshorts){ returns; } /* *Class:com_nedu_jni_jnidemo5_NativeMethod *Method:getInt *Signature:(I)I */ JNIEXPORTjintJNICALLJava_com_nedu_jni_jnidemo5_NativeMethod_getInt (JNIEnv*e,jobjectthiz,jinti){ returni; } /* *Class:com_nedu_jni_jnidemo5_NativeMethod *Method:getLong *Signature:(J)J */ JNIEXPORTjlongJNICALLJava_com_nedu_jni_jnidemo5_NativeMethod_getLong (JNIEnv*e,jobjectthiz,jlongl){ returnl; } /* *Class:com_nedu_jni_jnidemo5_NativeMethod *Method:getFloat *Signature:(F)F */ JNIEXPORTjfloatJNICALLJava_com_nedu_jni_jnidemo5_NativeMethod_getFloat (JNIEnv*e,jobjectthiz,jfloatf){ returnf; } /* *Class:com_nedu_jni_jnidemo5_NativeMethod *Method:getDouble *Signature:(D)D */ JNIEXPORTjdoubleJNICALLJava_com_nedu_jni_jnidemo5_NativeMethod_getDouble (JNIEnv*e,jobjectthiz,jdoubled){ returnd; } 第五步:制作mk文件 LOCAL_PATH:=$(callmy-dir) include$(CLEAR_VARS) LOCAL_MODULE:=com_nedu_jni_jnidemo5-jni LOCAL_SRC_FILES:=NativeMethod.c include$(BUILD_SHARED_LIBRARY) 第六步:调用native方法 publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); TextViewtext=(TextView)findViewById(R.id.text); NativeMethodmethod=newNativeMethod(); 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基本类型可以直接使用。 本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/817170,如需转载请自行联系原作者