简体   繁体   English

Android AlertDialog 直到 bluetoothsocket.connect() 之后才会显示

[英]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.我有一个AlertDialog设置为在bluetoothsocket.connect()之前显示,这是一种阻塞方法。 However, the AlertDialog doesn't show until after the bluetoothsocket.connect() method finishes.但是, AlertDialog直到bluetoothsocket.connect()方法完成后才会显示。

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?什么可能导致这种行为?

If your bluetoothsocket.connect() is blocking, which you said it is, you should put it out of the UI main thread.如果你的bluetoothsocket.connect()被阻塞,你说它是,你应该把它放在 UI 主线程之外。 What you can do is put it inside an AsyncTask .你可以做的是把它放在一个AsyncTask 中 Your myalertdialog.show() can be executed right before calling your AsyncTask .您的myalertdialog.show()可以在调用AsyncTask之前执行。 Then call myalertdialog.hide() in AsyncTask 's onPostExecute() .然后在AsyncTaskmyalertdialog.hide()中调用myalertdialog.hide() onPostExecute()

Since bluetoothsocket.connect block UI calls its on separate thread由于 bluetoothsocket.connect 块 UI 在单独的线程上调用它

final Handler mHandler = new Handler();// This statement is to be called by the main thread

                myalertdialog.show();

                Thread t = new Thread(
                        new Runnable(){

                            public void run()
                            {

                                mybluetoothsocket.connect(); 
                                mHandler.post(new Runnable(){

                                    public void run()
                                    {
                                        //ProgressDialog.dismiss();
                                    }
                                });
                            }});
                t.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序挂在BluetoothSocket.connect()上,挂在InputStream.read()上,connect()后无法断开连接 - Application hangs on BluetoothSocket.connect(), hangs on InputStream.read(), can't disconnect after connect() BluetoothSocket.connect()抛出IOException - BluetoothSocket.connect() throws IOException 带有Android 9的Samsung S9上BluetoothSocket.connect()失败 - BluetoothSocket.connect() fails on Samsung S9 with Android 9 为什么在Toast.makeText()。show()之前调用BluetoothSocket.connect()? - Why is BluetoothSocket.connect() called before Toast.makeText().show()? bluetoothsocket.connect()在第二次运行时引发异常 - bluetoothsocket.connect() throws exception in the second run BluetoothSocket.connect()引发异常“读取失败” - BluetoothSocket.connect() throwing exception “read failed” Android BluetoothSocket.connect()引发IOExceptions“连接被拒绝”和“服务发现失败” - Android BluetoothSocket.connect() throws IOExceptions “Connection Refused” and “Service discovery failed” 在Android的蓝牙中,BluetoothSocket.connect是否调用SDP以获取新频道 - In Android's bluetooth, does BluetoothSocket.connect invoke SDP to get fresh channel Android中的BluetoothSocket无法连接 - BluetoothSocket in Android doesn't connect Android BluetoothSocket无法连接 - Android BluetoothSocket can't connect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM