Android Development Practice: JNI Layer Thread callback Java function example

Source: Internet
Author: User

Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://ticktick.blog.51cto.com/823160/1358558

JNI is the abbreviation for Java Native interface, which is an important feature of the Java platform, making it easy for Java code to interact with the dynamic-link libraries generated by C + + code compilation. This article mainly gives a sample code (see the project file attached), describes how to open a thread in the JNI layer of Android, and callback the Java layer function in the thread.

The code is mainly divided into Java layer (Java code) and JNI layer (c language code), first look at the Java Layer Code (NATIVE.JAVA).

As shown above, the interface code of the Java layer and the JNI layer is mainly encapsulated in the native class, which defines three native functions, completes the initialization of the JNI library, calls the JNI layer to open the thread, calls the JNI layer to turn off the thread and so on. It also provides a callback function (Onnativecallback) that is called by the JNI layer and prints the value of count in the callback function.

Looking at how the JNI layer opens threads and calls back to the Java layer (NATIVE.C), the key points are commented in the code:

Native C implementation:

1. header file inclusion and definition of global variables

2. Implementation of the initialization function

3. Turn on the shutdown thread implementation

4. Threading implementation (key)

Native C + + implementation

#include <jni.h>
#include <android/log.h>
#include <pthread.h>
#include <unistd.h>

#define TAG "Jninative"
#define Logi (...) __android_log_print (android_log_info,tag,__va_args__)

JAVAVM *GJAVAVM;
Jobject gjavaobj;
int volatile gisthreadstop = 0;
static const char *classpath = "Com/jni/test/jninativecallback";

Static void* native_thread_exec (void *arg)
{
Logi ("Nativethreadexec");
JNIEnv *env;
Get env from GJAVAVM
Gjavavm->attachcurrentthread (&env,null);
Get Java class by ClassPath
Jclass thiz = Env->findclass (ClassPath);
Jclass thiz = Env->getobjectclass (gjavaobj);
Get Java method from Thiz
Jmethodid nativecallback = Env->getmethodid (Thiz, "Nativecallback", "(I) V");
int count = 0;
while (!gisthreadstop)
{
Sleep (2);
Env->callvoidmethod (thiz,nativecallback,count++);
Env->callvoidmethod (gjavaobj,nativecallback,count++);
}
Gjavavm->detachcurrentthread ();
Logi ("Thread stoped");
}


Jniexport void Jnicall native_init (jnienv *env,jobject thiz)
{
Logi ("Native_init");
gisthreadstop = 0;
ENV-&GT;GETJAVAVM (&AMP;GJAVAVM);
Gjavaobj = Env->newglobalref (thiz);
}

Jniexport void Jnicall Native_thread_start (jnienv *env,jobject jthiz)
{
Logi ("Native_thread_start");
gisthreadstop = 0;

pthread_t ID;
if (Pthread_create (&id,null,native_thread_exec,null)!=0)
{
Logi ("Native thread create fail");
Return
}

Logi ("Native thread creat success");
}

Jniexport void Jnicall native_thread_stop (jnienv *env,jobject jthiz)
{
Gisthreadstop = 1;
Logi ("Native_thread_stop");
}


Static Jninativemethod methods[] = {
{"Nativeinit", "() V", (void*) Native_init},
{"Nativethreadstart", "() V", (void*) Native_thread_start},
{"Nativethreadstop", "() V", (void*) Native_thread_stop}
};

Jint jni_onload (JAVAVM *vm, void *reserve)
{
Logi ("Jni_onload");
JNIEnv *env;
if (vm->getenv (void**) &env,jni_version_1_4)!=JNI_OK)
{
return-1;
}

Jclass jthiz = Env->findclass (ClassPath);

if (Env->registernatives (Jthiz,methods,sizeof (methods)/sizeof (Methods[0])) <0)
{
return-1;
}
return jni_version_1_4;
}

As can be seen from the above code, the JNI layer through the Pthread library to complete the creation of threads, it is important to note that the JNI layer of the thread, must be through the global JAVAVM to get to the environment variables, but also through the global jobject to get the Java class object, to find the Java side of the function , make a callback.

Android Development Practice: JNI Layer Thread callback Java function example

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.