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.)

Asked by: Adelaide843 | Posted: 25-01-2022

Answer 1

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");

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);

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

hope it helps


Answered by: Julian344 | Posted: 26-02-2022

Answer 2

You definitely don't want two UI threads. There should be only one thread that communicates with the Android SDK as far as the control flow and display go (i.e. anything related to drawing, starting activities, displaying dialogs, etc).

Also, keep in mind that you don't want to actually keep your thread running - everything is based on events, so you want your code to respond to something, do something, and then exit as soon as possible.

When you say "block", what exactly do you mean? What needs to be blocked? If you simply need to stop responding to events, why not have a boolean that is set to true while the dialog is visible, and simply ignore all events while it is true?

Answered by: Caroline337 | Posted: 26-02-2022

Similar questions

Android onClick blocking onFling

I have an Activity that implements a Gesture Detector to catch the user fling input for navigation to other screens. That was working fine - but - I recently updated a class that derives from BaseActivity to add an onClick function and now that click event seems to block the onFling from being hit. The onClick is tied to a TextView area (in a LinearLayout) I have on my screen. The resultsClick method is wired to the Tex...

java - Blocking an Android thread with a dialog

I have an Android application that connects to Bluetooth devices somewhat freely. When I attempt to connect to a device that isn't paired with the phone, a dialog will prompt the user for a passkey. The passkey is known a priori, but as there is no method to programatically enter it (in the standard Android APIs at least), it must be shown to the user then entered. What I have presently is a thread that initiates...

android - How Do I Block Adfree from blocking ads in my apps?

java - Waiting for thread to finish without blocking UI thread

Im running a Thread inside methode and i want to return a value once the thread finish, the problem that i tried to do join() but that blocks the UI thread. How could i wait for the thread to finish and then return the value without blocking the UI thread ? Boolean foo(){ myThread mt = new myThread(); mt.start(); return mt.isSentSuccessfully; }

java - Avoid blocking with state variable copies?

I'd like some wisdom regarding a recent multithreading idea of mine. Here goes: Suppose I have the following (pseudo) class whose run() method is chugging away forever on some thread. Other threads will at random times change the state of the Foo instance using setState(). The work that run() is doing only involves reading the state variables, no writing, and the ...

Blocking websites on Android

Is there any way of blocking websites programmatically on an Android device? I would like to have a blacklist of URLs that are populated into a continually running service. When the user running under restricted terms opens a browser, it should check that blacklist. I've thought of modifying the hosts.allow and hosts.deny files, but those are protected.

Call Blocking in Android 2.3

I am developing an application to block an incoming call but as far as I know in Android 2.3 gingerbread google has disabled the feature of: <uses-permission android:name="android.permissi​on.MODIFY_PHONE_STATE" />. So now my app only runs on models below 2.3. Can any one help me on this topic?

java - HTTP request in Async Task is blocking the UI

I'm making an HTTP request to download a file. This request for the data as well as writing the data to disk is done within an AsyncTask. However, randomly when downloading the file the application will freeze for 5-10 seconds while the next block of data is read using Since this is in an AsyncTask, the application should not freeze, correct? The follo...

android - Blocking access to my app's database

I need to block user access to my app's data stored in the SD card... like the images etc as they are crucial to my app's proper functioning and if deleted by mistake, will cause the application to function way different from what is expected of it. Is there any way to do so programmatically, like when I create this directory structure during my first run, lock the access to it to be only unlocked when the app runs?

Blocking outgoing SMS/MMS in android

I have Read Incoming SMS Content and Blocked the same before entering in to the inbox. The code is Given below: import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; import android.widget.Toast; public class BroadCastReceiver extends BroadcastReceiver { /** Called when the activity is firs...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android