Handler在主线程和子线程

handler用来处理一些费时的操作,数据查询,连接网络等。
handler的机制:handler包含handler,通过sendEmptyMessage将数据发送到Messagequen中,Looper从Messagequen中拿到我们的message。这里要实现的功能是点击按钮模拟数据的发送,我们点击一个按钮就生成一个线程,在线程中实现数据的发送。发送到handler中的handleMessage。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    String src = (String) msg.obj;
                    textView.setText(src);
                    break;
                case 1:
                    break;
                case 2:
                    break;
            }
        }
    };
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        textView = findViewById(R.id.textView);

    }



    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //点击一次按钮,请求一个数据
            case R.id.button1:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message message = new Message();
                        message.what = 0;
                        message.obj = "1323098878";
                        handler.sendMessage(message);
                    }
                }).start();
                break;
            case R.id.button2:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                    }
                }).start();
                break;
            case R.id.button3:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                    }
                }).start();
                break;
        }
    }
}

在子线程中,我们在主线程中,模拟耗时操作。

public class MainActivity extends AppCompatActivity {//handler
    Handler handler = new Handler() {//主线程中的Handler

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(new Runnable() {//开启一个子线程,模拟耗时操作
            @Override
            public void run() {
                SystemClock.sleep(2000);
                //发送消息到主线程
                handler.sendEmptyMessage(0);
                //调用Looper.preare();
                Looper.prepare();
                handlerThread = new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                    }
                };
                handlerThread.sendEmptyMessage(1);
                Looper.loop();//将handlerThread加入到Looper中
            }
        }).start();

你可能感兴趣的:(Handler在主线程和子线程)