duckduckgo
duckduckgo

Reputation: 1295

Calling webview method from JNI

I subclassed WebView and added a native methods in that call which is implemented as:

JNIEXPORT jboolean JNICALL Java_com_mypackage_openUrl(
        JNIEnv* env, jobject webView, jstring url) {
    LOGI("openUrl");

    jclass webViewClass = env->GetObjectClass(webView);
    if (!webViewClass) {
        LOGE("webView class not found");
        return false;
    }

    jmethodID method = env->GetMethodID(webViewClass, "loadUrl",
            "(Ljava/lang/String;)V");

    env->CallObjectMethod(webViewClass, method, url);

}

But it does not work and application would crash, I know that WebView method must be called from UI(main) thread, but still I need to understand if this possible from JNI side?

Upvotes: 1

Views: 1087

Answers (1)

LaurentY
LaurentY

Reputation: 7653

You have to use CallBack mechanism to send command+data from c/c++ to java class. Many tutorials exist, you could also search on stackoverflow.

For instance: How to create callbacks between android code and native code?

Upvotes: 1

Related Questions