Android JNI Introductory article fifth-basic data type usage

Source: Internet
Author: User

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?
  1. Public class Nativemethod {
  2. Static {
  3. System.loadlibrary ("Com_nedu_jni_jnidemo5-jni");
  4. }
  5. public Native boolean Getboolean (boolean B);
  6. public native byte getbyte (byte b);
  7. public native Char GetChar (char c);
  8. public native short getshort (shorts);
  9. public Native int getInt (int i);
  10. public native long Getlong (long L);
  11. public native float getfloat (float f);
  12. public native double getdouble (double D);
  13. }

Part III: Call the Javac, Javah command to generate the H file.

Fourth: Supplementary native method, as follows:

[CPP]View Plaincopyprint?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "Com_nedu_jni_jnidemo5_nativemethod.h"
  4. /*
  5. * Class:com_nedu_jni_jnidemo5_nativemethod
  6. * Method:getboolean
  7. * Signature: (z) z
  8. */
  9. Jniexport Jboolean Jnicall Java_com_nedu_jni_jnidemo5_nativemethod_getboolean
  10. (JNIEnv *e, Jobject thiz, Jboolean b) {
  11. return B;
  12. }
  13. /*
  14. * Class:com_nedu_jni_jnidemo5_nativemethod
  15. * Method:getbyte
  16. * Signature: (b) b
  17. */
  18. Jniexport Jbyte Jnicall Java_com_nedu_jni_jnidemo5_nativemethod_getbyte
  19. (JNIEnv *e, Jobject thiz, jbyte by) {
  20. return by;
  21. }
  22. /*
  23. * Class:com_nedu_jni_jnidemo5_nativemethod
  24. * Method:getchar
  25. * Signature: (c) C
  26. */
  27. Jniexport Jchar Jnicall Java_com_nedu_jni_jnidemo5_nativemethod_getchar
  28. (JNIEnv *e, Jobject thiz, Jchar c) {
  29. return C;
  30. }
  31. /*
  32. * Class:com_nedu_jni_jnidemo5_nativemethod
  33. * Method:getshort
  34. * Signature: (s) s
  35. */
  36. Jniexport Jshort Jnicall Java_com_nedu_jni_jnidemo5_nativemethod_getshort
  37. (JNIEnv *e, Jobject thiz, Jshort s) {
  38. return s;
  39. }
  40. /*
  41. * Class:com_nedu_jni_jnidemo5_nativemethod
  42. * Method:getint
  43. * Signature: (i) I
  44. */
  45. Jniexport Jint Jnicall Java_com_nedu_jni_jnidemo5_nativemethod_getint
  46. (JNIEnv *e, Jobject thiz, jint i) {
  47. return i;
  48. }
  49. /*
  50. * Class:com_nedu_jni_jnidemo5_nativemethod
  51. * Method:getlong
  52. * Signature: (j) J
  53. */
  54. Jniexport Jlong Jnicall Java_com_nedu_jni_jnidemo5_nativemethod_getlong
  55. (JNIEnv *e, Jobject thiz, Jlong L) {
  56. return L;
  57. }
  58. /*
  59. * Class:com_nedu_jni_jnidemo5_nativemethod
  60. * Method:getfloat
  61. * Signature: (f) F
  62. */
  63. Jniexport jfloat Jnicall Java_com_nedu_jni_jnidemo5_nativemethod_getfloat
  64. (JNIEnv *e, Jobject thiz, Jfloat f) {
  65. return F;
  66. }
  67. /*
  68. * Class:com_nedu_jni_jnidemo5_nativemethod
  69. * method:getdouble
  70. * Signature: (d) d
  71. */
  72. Jniexport jdouble Jnicall java_com_nedu_jni_jnidemo5_nativemethod_getdouble
  73. (JNIEnv *e, Jobject thiz, jdouble D) {
  74. return D;
  75. }

Fifth step: Making MK Files

[Plain]View Plaincopyprint?
    1. Local_path: = $ (call My-dir)
    2. Include $ (clear_vars)
    3. Local_module: = Com_nedu_jni_jnidemo5-jni
    4. Local_src_files: =NATIVEMETHOD.C
    5. Include $ (build_shared_library)


Sixth step: Call the native method

[Java]View Plaincopyprint?
  1. Public void OnCreate (Bundle savedinstancestate) {
  2. super.oncreate (savedinstancestate);
  3. Setcontentview (R.layout.main);
  4. TextView text= (TextView) Findviewbyid (R.id.text);
  5. Nativemethod method=New Nativemethod ();
  6. Text.settext ("return Boolean:" +method.getboolean (True) +"\ n" +
  7. "return byte:" +method.getbyte ((byte) 0) +"\ n" +
  8. "return char:" +method.getchar (' C ') +"\ n" +
  9. "return short:" +method.getshort ((short) 1) +"\ n" +
  10. "return int:" +method.getint (1) +"\ n" +
  11. "Return Long:" +method.getlong (9) +"\ n" +
  12. "return float:" +method.getfloat ((float) 1.0) +"\ n" +
  13. "return double:" +method.getdouble (2.0) +"\ n");
  14. }

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

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.