Android处理程序操作未被处理

时间:2013-01-05 12:26:59

标签: android android-handler

我正在尝试将消息从Thread传递给Handler但是,处理程序switch语句的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();
        }
    }

};

这是Thread run()方法,其中消息被传递给Handler。 Thread是一个内部类。

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--");
    }

我不确定为什么没有采取这些行动。

1 个答案:

答案 0 :(得分:0)

通过查看代码很难说出了什么问题。基本上,第二个线程需要引用第一个线程的处理程序。但是,您的第二个线程中的mHandler引用与主线程中的mHandler对象的关联尚不清楚。他们属于同一个班吗?第二个线程是包含mHandler对象的类的内部类吗?

由于我很想自己做一个Handler Message实现,我写了一个小例子。也许以下示例将帮助您:

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();
    }
}

}