JNI in Android Studio--2--Writing C files

Source: Internet
Author: User

In the previous article, we wrote 2 methods in the native interface

Generate the corresponding. h file

At this time, we need to improve our own. C File

/* Don't EDIT this file-it are machine generated */
#include <jni.h>
#include <android/log.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


/* Header for class LED_OKI_COM_JNIDEMO_APPJNI */

#ifndef _included_led_oki_com_jnidemo_appjni
#define _included_led_oki_com_jnidemo_appjni
#ifdef __cplusplus
extern "C" {
#endif

This section is constant, fixed


#define TAG "jnidemo-jni-test"//This is the identity of the custom log
#define LOGD (...)

This section is primarily a custom log ID, equivalent to the LOG.D () method in Android


Method One: Getstringfromnative
Jniexport jstring Jnicall java_com_oki_led_jni_appjni_getstringfromnative
(JNIENV * env, Jobject jobj) {
Logd ("Log string from NDK.");
Return Env->newstringutf (env, "Hello from jni!");
}

This paragraph, corresponding to one of our native writing methods Getstringfromnative, the custom log use is using the custom method name LOGD ("Hint content")
This method means to return a string type of data that is the Hello from jni!
This string can be received on Android
Appjni Appjni = new Appjni ();
String msg = appjni.getstringfromnative ();
At this point, the MSG receives the data is the Hello from jni!


Method Two: Docoordtest (COORD COORD)

Jniexport Jobject Jnicall Java_com_oki_led_jni_appjni_docoordtest
(jnienv *env, Jobject obj, Jobject coord) {
Jclass Coordclass = (*env)->getobjectclass (env, coord);
if (Coordclass) {
Jboolean iscopy;
Jfieldid xId = (*env)->getfieldid (env, Coordclass, "X", "I");
Jint x = (int) (*ENV)->getintfield (env, coord, xId);
LOGD ("x =%d", x);

Jfieldid Yid = (*env)->getfieldid (env, Coordclass, "Y", "I");
Jint y = (int) (*ENV)->getintfield (env, coord, Yid);
Logd ("y =%d", y);

Jfieldid Widthid = (*env)->getfieldid (env, Coordclass, "width", "I");
Jint width = (int) (*ENV)->getintfield (env, coord, Widthid);
LOGD ("width =%d", width);

Jfieldid Heightid = (*env)->getfieldid (env, Coordclass, "height", "I");
Jint height = (int) (*ENV)->getintfield (env, coord, Heightid);
LOGD ("height =%d", height);
}

Jclass cls = (*env)->findclass (env, "Com/oki/led/jni/nativeobj/coord");
Jmethodid id = (*env)->getmethodid (env, CLS, "<init>", "() V");

Jobject paramout = (*env)->newobjecta (env, CLS, ID, 0);

return paramout;
}

Where Coord is a Coord object class created in Android
public class COORD {
public int x;
public int y;
public int width;
public int height;

/**
* Set the initial value
* Why to set the initial value: prevent unnecessary problems such as data error or content overflow in C file, also reduce the judgment of data in C file, lazy method (can not be used)
*/
Public COORD () {
x = 0;
y = 0;
width = 0;
Height = 0;
}
}
If a class is passed in the native, the data in the C file should initially receive the Jobject type, converted to the Jclass type by the Jobject type, and the values in the class

Jclass Coordclass = (*env)->getobjectclass (env, coord);

Fragment 1: Taking a value

Take the value x ID in the class
Jfieldid xId = (*env)->getfieldid (env, Coordclass, "X", "I");
Converts the x ID to the corresponding type of x value
Jint x = (int) (*ENV)->getintfield (env, coord, xId);
Print x data to determine if the value is correct during Android operation
LOGD ("x =%d", x);

Fragment 2: Calling the initial method

Find Class class COORD
Jclass cls = (*env)->findclass (env, "Com/oki/led/jni/nativeobj/coord");
Call its initialization method on this class
Jmethodid id = (*env)->getmethodid (env, CLS, "<init>", "() V");
To convert data from a CLS class to a Jobject class
Jobject paramout = (*env)->newobjecta (env, CLS, ID, 0);
Return the data out
return paramout;

Fragment 3: Assigning values

If you need to return the value in fragment 1, you need to create a new Jobject method to host the data
Find Class class COORD
Jclass cls = (*env)->findclass (env, "Com/oki/led/jni/nativeobj/coord");
But you don't have to.
Jmethodid id = (*env)->getmethodid (env, CLS, "<init>", "() V");
To convert data from a CLS class to a Jobject class
Jobject paramout = (*env)->newobjecta (env, CLS, ID, 0);

Jfieldid xId = (*env)->getfieldid (env, Coordclass, "X", "I");
Assigns a XID to a value of 3
(*env)->setintfield (env, Paramout, XId, 3);

return paramout;

Basically, the question remains: is there a more convenient way to assign a value? Is there some code duplication inside?

JNI in Android Studio--2--Writing C files

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.