Skip to content

Instantly share code, notes, and snippets.

@whatisor
Last active June 10, 2021 03:43
Show Gist options
  • Save whatisor/e114606079e5c99752ea5678429800b6 to your computer and use it in GitHub Desktop.
Save whatisor/e114606079e5c99752ea5678429800b6 to your computer and use it in GitHub Desktop.
How to call Java function from jni
1. Jni:
extern "C" JNIEXPORT void JNICALL
Java_com_example_javaactivity_initApplicationNative(
JNIEnv* env, jobject obj)
{
__android_log_print(ANDROID_LOG_VERBOSE, "jni", "jni initApplicationNative");
env->GetJavaVM(&javaVM);
jclass cls = env->GetObjectClass(obj);
activityClass = (jclass) env->NewGlobalRef(cls);
activityObj = env->NewGlobalRef(obj);
}
void startCamera(jint a,jint b, jint c)
{
JNIEnv *env;
javaVM->AttachCurrentThread(&env, NULL);
jmethodID method = env->GetMethodID(activityClass, "jniCallReady", "(III)V");
env->CallVoidMethod(activityObj, method,a,b,c);
}
2.Java:
initApplicationNative();//init interface in activity
define function:
public void jniCallReady(int a, int b, int c){
//do st
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment