Android处理程序未使用HandlerThread接收消息

时间:2019-02-05 08:26:25

标签: java android handler message-queue android-handlerthread

我在下面的代码中使用 HandlerThread Handler ,实际上,我尝试将数据从可运行更新为Handler回调。

在每个循环中,都调用了 sendMessage()函数,但在Callback中没有收到任何虚假消息。 我缺少什么?

public class MainActivity extends AppCompatActivity {

    private TextView result;
    HandlerThread handlerThread = new HandlerThread("bThread");

    Callback callBack = new Callback(){

        @Override
        public boolean handleMessage(Message message) {
            Bundle bundle = message.getData();
            String val = bundle.getString("output");
            result.setText(val);

            return false;
        }
    };
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        result = findViewById(R.id.result);
        handlerThread.start();
        handler = new Handler(handlerThread.getLooper(),callBack);
        handler.post(runnable);
    }

    Runnable runnable= new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i <200 ; i++) {
                try {
                    Message message = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putString("output",i+"");
                    message.setData(bundle);
                    handler.sendMessage(message);
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

}

1 个答案:

答案 0 :(得分:0)

我认为您的代码可以正常工作

问题在下面的行

             Thread.sleep(3000);

注释此行并运行您的代码,或者像下面的代码一样减少一些时间。

    package com.myapplication;

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.HandlerThread;
    import android.os.Message;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class HandleActivity extends Activity implements View.OnClickListener {

        private TextView tvMessage;
        private Button btSendMesaage;
        private HandlerThread handlerThread = new HandlerThread("hthread");

        private Handler.Callback callback = new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                Bundle bundle = msg.getData();
                String messsage = bundle.getString("output");
                tvMessage.setText(messsage);
private Handler.Callback callback = new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Bundle bundle = msg.getData();
            String messsage = bundle.getString("output");
            tvMessage.setText(messsage);
            try{
            Thread.sleep(3000);}catch (Exception e){}
            return false;
        }
    };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.handler_activity);
            initLayout();
            handlerThread.start();
        }

        private void initLayout() {
            tvMessage = (TextView) findViewById(R.id.textview);
            btSendMesaage = (Button) findViewById(R.id.button);
            btSendMesaage.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button:
                    sendMessage();
                    break;
                default:
                    break;
            }
        }

        private Handler handler;

        private void sendMessage() {

            handler = new Handler(handlerThread.getLooper(), callback);
            handler.post(runnable);
        }

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 200; i++) {
                    try {
                        Message message = new Message();

                        Bundle bundle = new Bundle();
                        bundle.putString("output", i + "");
                        message.setData(bundle);

                        handler.sendMessage(message);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        };
    }