Android的处理程序的行为没有得到处理 [英] Android Handler actions not being processed

查看:205
本文介绍了Android的处理程序的行为没有得到处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试然而,从一个线程传递一个消息处理程序,处理程序switch语句的处理程序的行为永远不会被处理。

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.

这是我的处理程序:

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()方法。线程是一个内部类。

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.

推荐答案

这是很难说什么是错了看你的code。基本上,第二线程需要第一线程的处理程序的引用。但是,您的mHandler参考在你的第二个线程在你的主线程关联到你的mHandler对象尚不清楚。难道他们属于同一类?是第二个线程一个内部类包含mHandler对象的类的?

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

}

这篇关于Android的处理程序的行为没有得到处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆