主线程和子线程之间传递消息

在这里插入图片描述



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置一个整数"/>

    <EditText
        android:id="@+id/et_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算结果为"/>
    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开始计算"/>
</LinearLayout>


public class MainActivity extends AppCompatActivity {

    private static final int NUMBER = 1;
    private static final int RESULT = 0;
    private MyThread myThread;
    private TextView tvResult;
    private EditText etNum;
    //主线程Handler
    private Handler mainHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //处理Message,运行在主线程,可以修改界面元素
            switch (msg.what) {
                case RESULT:
                    long res = (long) msg.obj;
                    tvResult.setText(res + "");
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvResult = findViewById(R.id.tv_result);
        etNum = findViewById(R.id.et_num);
        //创建子线程
        myThread = new MyThread();
        myThread.start();
        Button button = findViewById(R.id.btn_start);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = etNum.getText().toString();
                //向子线程发送数据
                Message message = new Message();
                message.what = NUMBER;
                message.obj = Integer.parseInt(str);
                workHandler.sendMessage(message);
            }
        });
    }

    //子线程的Handler
    private Handler workHandler;

    private class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            //子线程中使用Handler要手动开启Looper
            Looper.prepare();
            workHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    //处理来自主线程的数据
                    switch (msg.what){
                        case NUMBER:
                            long res = fib((int)msg.obj);
                            //发送Message
                            Message message = new Message();
                            //添加数据
                            message.obj = res;
                            //添加标识
                            message.what = RESULT;
                            mainHandler.sendMessage(message);
                            break;
                    }

                }
            };
            Looper.loop();

            //不能直接在子线程中修改UI
//            tvResult.setText(res+"");
        }
    }

    private long fib(int n) {
        if (n == 1) {
            return 1;
        } else if (n == 2) {
            return 1;
        } else {
            return fib(n - 1) + fib(n - 2);
        }
    }
}

展开阅读全文

没有更多推荐了,返回首页