With the Java and native data types in front of you, here's where to start:
First Step: New project
Part II: Writing Java methods:
[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 (shorts);
- public Native int getInt (int i);
- public native long Getlong (long L);
- public native float getfloat (float f);
- public native double getdouble (double D);
- }
Part III: Call the Javac, Javah command to generate the H file.
Fourth: Supplementary native method, as follows:
[CPP]View Plaincopyprint?
- #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;
- }
Fifth step: Making MK Files
[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)
Sixth step: Call the native method
[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 ("return Boolean:" +method.getboolean (True) +"\ n" +
- "return byte:" +method.getbyte ((byte) 0) +"\ n" +
- "return char:" +method.getchar (' C ') +"\ n" +
- "return short:" +method.getshort ((short) 1) +"\ n" +
- "return int:" +method.getint (1) +"\ n" +
- "Return Long:" +method.getlong (9) +"\ n" +
- "return float:" +method.getfloat ((float) 1.0) +"\ n" +
- "return double:" +method.getdouble (2.0) +"\ n");
- }
Run:
Summary: The Java basic types passed in JNI can be used directly.
/**
* @author Zhang Xingye
* Email: xy-zhang#163.com
* Android Development Advanced Group: 278401545
*
*/
[Java]View Plaincopyprint?
Android JNI Introductory article fifth-basic data type usage