JNI creates java class and returns

1, define a static method in java

//我们就在MainActivity中定义一个静态的方法
    public static void  JNIcallJava(){
        Log.i("test class","when you have seen this you are sucess");
    }

2, JNI uses this method

extern "C"
JNIEXPORT void JNICALL
Java_com_example_administrator_myjnitest_MainActivity_callMethod(JNIEnv *env, jobject instance) {
    LOGI("now begin to call the java static method");
    // 这里是查找该方法在java的那个类中,注意这里的包名路径要写正确,精确到类名
    jclass class_method = env->FindClass("com/example/administrator/myjnitest/MainActivity");
    //我们根据类名来查找具体的方法,注意下面的第三个参数表示无参数无返回值void
    jmethodID  md = env->GetStaticMethodID(class_method,"JNIcallJava","()V");
    //调用这个java的静态方法
    env->CallStaticVoidMethod(class_method,md);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325577499&siteId=291194637