0
我正在嘗試將消息從線程傳遞到處理程序,但處理程序切換語句的處理程序操作從未得到處理。未處理Android處理程序操作
這是我的處理程序:
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch(msg.what) {
case SUCCESS:
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
String s = "Successfully Connected";
connectedThread.write(s.getBytes());
break;
case MESSAGE_READ:
byte[] readBuff = (byte[])msg.obj;
String string = new String(readBuff);
Toast.makeText(getApplicationContext(), string, 0).show();
}
}
};
這是該消息被傳遞給處理程序的線程run()方法。線程是一個內部類。
public void run() {
if (D) Log.e(TAG, "-- ConnectThread --");
// Cancel discovery because it will slow down the connection
mBtAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
mHandler.obtainMessage(SUCCESS, mmSocket).sendToTarget();
if (D) Log.e(TAG, "--SUCCESS--");
}
我不確定爲什麼這些操作沒有執行。
是的,它們屬於同一類。線程是一個內部類。 – user1909680
我的代碼有幫助嗎?如果不是,那麼你應該發佈更多的代碼。 – joel
是的,它確實有幫助。謝謝 – user1909680