[英]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()
.然后在AsyncTask
的myalertdialog.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.