2016-10-06 10 views
1

AlertDialogは、ブロック方法であるbluetoothsocket.connect()の前に表示するように設定されています。ただし、bluetoothsocket.connect()メソッドが終了するまで、AlertDialogは表示されません。AndroidのAlertDialogは、bluetoothsocket.connect()の後に表示されません。

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

この現象の原因は何ですか?

答えて

1

bluetoothsocket.connect()がブロックしていると言われた場合は、UIのメインスレッドから除外する必要があります。あなたができることはAsyncTaskの中に入れます。 myalertdialog.show()は、AsyncTaskに電話する前に実行することができます。次にmyalertdialog.hide()AsyncTaskonPostExecute()に呼び出します。 bluetoothsocket.connectブロックUIを呼び出すので

+0

は、ブロッキングコードがUIスレッド上で実行するべきではありません。あなたが言及したように私はそれをAsyncTaskに移しました。そして今は完全に動作します。ありがとう!! – pcdangio

0

その別のスレッドであなたが正しい

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(); 
関連する問題