在使用bluetoothsocket.connect()之后,Android AlertDialog不会显示

时间:2016-10-06 04:10:17

标签: android bluetooth alertdialog

我有AlertDialog设置为bluetoothsocket.connect()之前显示,这是一种阻止方法。但是,在AlertDialog方法完成之后,bluetoothsocket.connect()才会显示。

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

可能导致此行为的原因是什么?

2 个答案:

答案 0 :(得分:1)

如果你的bluetoothsocket.connect()被阻止,你说它是,你应该把它放在UI主线程之外。你能做的就是把它放在AsyncTask里面。您可以在致电myalertdialog.show()之前立即执行AsyncTask。然后在myalertdialog.hide()的{​​{1}}中致电AsyncTask

答案 1 :(得分:0)

由于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();