Created
May 28, 2015 08:25
-
-
Save Jiezhi/79c04de92762ed5ac890 to your computer and use it in GitHub Desktop.
how c/c++ call java method in android by jni
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "com_jiezhi_jnitest_MainActivity.h" | |
jclass mClass; | |
jobject mObject; | |
jmethodID mStaticmethodID, mnotStaticMethodID; | |
/* | |
* Class: com_jiezhi_jnitest_MainActivity | |
* Method: nativeInit | |
* Signature: ()V | |
*/ | |
JNIEXPORT void JNICALL Java_com_jiezhi_jnitest_MainActivity_nativeInit( | |
JNIEnv *env, jobject thiz) | |
{ | |
jclass clazz = env->GetObjectClass(thiz); | |
mClass = (jclass) env->NewGlobalRef(clazz); | |
mObject = (jobject) env->NewGlobalRef(thiz); | |
mStaticmethodID = env->GetStaticMethodID(mClass, | |
"setStaticMethodByNativeCode", "(I)V"); | |
mnotStaticMethodID = env->GetMethodID(mClass, "setMethodByNativeCode", | |
"(I)V"); | |
} | |
/* | |
* Class: com_jiezhi_jnitest_MainActivity | |
* Method: stringFromJni | |
* Signature: ()V | |
*/ | |
JNIEXPORT void JNICALL Java_com_jiezhi_jnitest_MainActivity_nativeExecution( | |
JNIEnv *env, jobject thiz) | |
{ | |
env->CallVoidMethod(mObject, mnotStaticMethodID, 100); | |
} | |
/* | |
* Class: com_jiezhi_jnitest_MainActivity | |
* Method: nativeBusiness | |
* Signature: ()V | |
*/ | |
JNIEXPORT void JNICALL Java_com_jiezhi_jnitest_MainActivity_nativeStaticExecution( | |
JNIEnv *env, jobject thiz) | |
{ | |
env->CallVoidMethod(mObject, mStaticmethodID, 200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment