pcdangio pcdangio - 2 months ago 17
Android Question

Android AlertDialog won't show until after bluetoothsocket.connect()

I have an

AlertDialog
that is set to display before
bluetoothsocket.connect()
, which is a blocking method. However, the
AlertDialog
doesn't show until after the
bluetoothsocket.connect()
method finishes.

myalertdialog.show();
// Dialog is not shown.
mybluetoothsocket.connect(); // This blocks and takes a few seconds to run.
// Dialog is shown.


What could be causing this behavior?

Answer

If your bluetoothsocket.connect() is blocking, which you said it is, you should put it out of the UI main thread. What you can do is put it inside an AsyncTask. Your myalertdialog.show() can be executed right before calling your AsyncTask. Then call myalertdialog.hide() in AsyncTask's onPostExecute().