4

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

在每个循环中,调用了sendMessage()函数,但我在回调中没有收到任何内容。我错过了什么?

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

}
4

1 回答 1

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();
                    }
                }
            }
        };
    }
于 2019-02-07T06:01:00.363 回答