0

How I can call java method(non-static) from native(C++) code? I saw some examples on Internet ut don't understood exactly(it' dont works for me).

I trying something like this code:

java code:

public void displayInterstitial() {

            if(adView!=null)
            {

        if (adView.getVisibility() == AdView.VISIBLE) {
                adView.post(new Runnable() {
                    public void run() {
                        adView.setVisibility(AdView.INVISIBLE);
                    }
                });
            }
        }

    }

C++ code:

// JNI OnLoad
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
{
    g_JavaVM = vm;
    return JNI_VERSION_1_6;
}

JNIEnv* MenuState::getJniEnv() {
    JavaVMAttachArgs attachArgs;
    attachArgs.version = JNI_VERSION_1_6;
    //attachArgs.name = ">>>NativeThread__Any";
//  attachArgs.group = NULL;

    JNIEnv* env;
    if (g_JavaVM->AttachCurrentThread(&env, &attachArgs) != JNI_OK) {
        env = NULL;
    }

    return env;
}


void MenuState::displayInterstitial()
{

   JNIEnv *env = getJniEnv();

    jclass cls = env->FindClass("org/libsdl/app/SDLActivity");

  jmethodID dispin = env->GetMethodID(cls, "displayInterstitial", "()V");

     env->CallVoidMethod(obj, dispin);
}

Sorry for maybe duplicate question. How i can get jobject(obj) for this line env->CallVoidMethod(obj, dispin); ?

1 Answer 1

1

The easiest way is to create the object in Java, then pass the handle with JNI.

If you want to create the object in C++ code, just get the class as you do, find the constructor < init > function, and use env->NewObject(...)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.