The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Use UI thread from SDLThread in C++
Noxalus


Joined: 06 Mar 2014
Posts: 26
Location: France
Hello everyone Very Happy

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

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:

Code:

sdlActivity.runOnUiThread( new Runnable() {
    @Override
    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 Smile
Noxalus


Joined: 06 Mar 2014
Posts: 26
Location: France
I can't edit my post, but I obviously speak about SDL with Android.