Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocking Dialog from within JNI code

I'm writing an app that's basically a wrapper around a 250K JNI. The JNI (a game engine) has APIs like handle_penUp(int x, int y). Sometimes it needs to query the user from inside handle_penUp() (via callbacks into Java code), so the dialog I use to implement the query must block.

I understand that the main thread of execution can't block. So I've spawned a second thread that makes all the JNI calls that might result in callbacks that would need to block. Inside that second thread, when I need to put up a blocking dialog, I call startActivityForResult() and then acquire() on a semaphore. When onActivityResult() gets called on the main thread it calls release() on the same semaphore.

This works if my query is implemented as a new Activity, but not if I want to showDialog() within the existing Activity. Log messages tell me my thread needs a Looper. I'm adding one -- and will append info on whether it works -- but it feels as if I'm going down the wrong path here. What I need is a recipe for doing blocking dialogs (useful if only because every other platform has them and so ported code will often work that way.)

like image 721
Andy House Avatar asked Nov 06 '22 17:11

Andy House


1 Answers

It sound very close to a problem I had with setting visible/invisible some view from the touch thread.

the problem is that you can't do some operations on the GUI form another thread (which is your case)

what you need to do is to use a Handle in your main thread I declared it in the Activity

public static final Handler handlerVisibility = new Handler() {
    public void handleMessage(Message msg) {
        int visibility = msg.getData().getInt("visibility");
        view.setVisibility(visibility);
    }
};

I chose the option of public static so that I can access in anywhere (because I never have more that one call at a time and that I felt lazy to pass it along to the sub classes).

then what you want to do is send a message to this handler and since the Handler is in the same thread as the gui it works ^^

Message msg = MainActivity.handlerVisibility.obtainMessage();
    Bundle b = new Bundle();
            b.putInt("visibility", View.VISIBLE);
    msg.setData(b);
            MainActivity.handlerVisibility.sendMessage(msg);

That should solve your looper error and allow you to send GUI request from one thread to another

hope it helps

Jason

like image 190
Jason Rogers Avatar answered Nov 11 '22 19:11

Jason Rogers