[SDL] Use UI thread from SDLThread in C++

Noxalus noxalus at gmail.com
Wed Sep 10 06:53:44 PDT 2014

Hello everyone  :D 

I'm currently trying to instantiate a WebView above the SDL rendering with this code:


// Retrieve the JNI environment from SDL 
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();

// Retrieve the Java instance of the SDLActivity
jobject sdlActivity = (jobject)SDL_AndroidGetActivity();

// Get SDLActivity class from its instance
jclass sdlActivityClass = env->GetObjectClass(sdlActivity);

// Get the current context
jmethodID getContextMethod = env->GetStaticMethodID(sdlActivityClass, "getContext","()Landroid/content/Context;");
jobject context = env->CallObjectMethod(sdlActivity, getContextMethod);

// Instanciate a WebView
jclass webViewClass = env->FindClass("android/webkit/WebView");
jmethodID webViewCtr = env->GetMethodID(webViewClass, "<init>", "(Landroid/content/Context;)V");
jobject webView = env->NewObject(webViewClass, webViewCtr, context);

But when I try to run this code, I have this warning: Warning: A WebView method was called on thread 'SDLThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.

I saw that JNI have a method called AttachCurrentThread, but you can't choose the thread on which you want to be attached. 

In Java, I also saw that you can do something like that:


sdlActivity.runOnUiThread( new Runnable() {
    public void run() {
    // Execute some Java instructions on UI thread

But I would like to keep my logical code in C++ and this solution needs that I create a Runnable for each action that I want to perform on my WebView...

Do you know if there is a solution for my problem?

If someone here know how to pass function pointer from C++ to Java with JNI, that can do the job :)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20140910/783718aa/attachment.htm>

More information about the SDL mailing list