![](/img/trans.png)
[英]Application hangs on BluetoothSocket.connect(), hangs on InputStream.read(), can't disconnect after connect()
[英]Android AlertDialog won't show until after bluetoothsocket.connect()
我有一个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.
什么可能导致这种行为?
如果你的bluetoothsocket.connect()
被阻塞,你说它是,你应该把它放在 UI 主线程之外。 你可以做的是把它放在一个AsyncTask 中。 您的myalertdialog.show()
可以在调用AsyncTask
之前执行。 然后在AsyncTask
的myalertdialog.hide()
中调用myalertdialog.hide()
onPostExecute()
。
由于 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.