Android JNI entry 5-Basic Data Type usage

Source: Internet
Author: User

The Java and native data types are mentioned earlier. Here we will start to use them:

Step 1: Create a project

Part 2: Writing Java methods:

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);}

 

Part 3: Call the javac and javah commands to generate an H file.

Part 4: add the native method as follows:

#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;  }

Step 5: Create an MK File

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

 

Step 6: Call the NATIVE METHOD

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" + "returns byte:" + method. getbyte (byte) 0) + "\ n" + "returns CHAR:" + method. getchar ('C') + "\ n" + "Return short:" + method. getshort (short) 1) + "\ n" + "Return INT:" + method. getint (1) + "\ n" + "returned long:" + method. getlong (9) + "\ n" + "returns float:" + method. getfloat (float) 1.0) + "\ n" + "returns double:" + method. getdouble (2.0) + "\ n ");}

 

Run:

Summary: The Basic Java types passed in JNI can be directly used.

/**
* @ Author Zhang xingye
* Email: xy-Zhang # 163.com
* Android Development Group: 278401545
*
*/

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.