问题
I am attempting to pass a message from a Thread to the Handler however, the Handler actions of the handler switch statement are never being processed.
This is my Handler:
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();
}
}
};
This is the Thread run() method where the message is being passed to the Handler. The Thread is an inner class.
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--");
}
I'm unsure as to why these actions are not being carried out.
回答1:
It is difficult to say what is wrong by looking at your code. Basically, the second thread needs a reference to the handler of the first thread. However, the association of your mHandler reference in your second thread to your mHandler object in your main thread is unclear. Do they belong to the same class? Is the the second thread a inner class of the class containing the mHandler object?
As I was curious to do a Handler Message implementation by myself I wrote a small example. Maybe the following example will help you:
public class MessageHandlerActivity extends Activity {
public TextView tv;
private static final int SUCCESS = 0;
private static final int FAIL = 1;
private MessageActivityHandler mHandler = new MessageHandlerActivity.MessageActivityHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
tv.setText("waiting!");
setContentView(tv);
new Thread(new MessageSenderActivity()).start();
}
static class MessageActivityHandler extends Handler {
private final WeakReference<MessageHandlerActivity> mActivity;
MessageActivityHandler(MessageHandlerActivity activity) {
mActivity = new WeakReference<MessageHandlerActivity>(activity);
}
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case SUCCESS:
mActivity.get().tv.setText("juhu!");
break;
case FAIL:
mActivity.get().tv.setText("fail!");
break;
}
}
};
private class MessageSenderActivity implements Runnable {
@Override
public void run() {
mHandler.obtainMessage(SUCCESS).sendToTarget();
}
}
}
来源:https://stackoverflow.com/questions/14171772/android-handler-actions-not-being-processed