2012-06-26 31 views
1

我有一個簡短的問題。如何在不使用AsyncTask的情況下更新TextField?在我的Activity類中,我有一個這樣的功能:動態更新TextField而不使用AsyncTask

private void CheckBlueToothState(){ 

     if (bluetoothAdapter == null){ 
      status.setText("Bluetooth NOT support."); 
     }else{ 
      if (bluetoothAdapter.isEnabled()){ 
       if(bluetoothAdapter.isDiscovering()){ 
        status.setText("Bluetooth is currently in device discovery process."); 
       }else{ 
        status.setText("Bluetooth is Enabled"); 
        search.setEnabled(true); 
       } 
      }else{ 
       status.setText("Bluetooth is NOT Enabled"); 
       Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
      } 
     } 
    } 

它檢查設備是否獲得了藍牙,是啓用還是禁用,您希望獲得照片。所以再次回到我的問題。我如何動態更改文本框status如果我打開藍牙,反之亦然?

+0

嘗試使用處理程序更新UI線程中的文本? – jpa

回答

6

註冊BroadcastReceiver意向操作BluetoothAdapter.ACTION_STATE_CHANGED並將您的文本更新代碼移動到onReceive方法。

可以找到示例代碼here