Handler

作者: Amy木婉清 | 来源:发表于2020-09-01 10:04 被阅读0次

    1.关于Handler延时处理机制:

        mHandler = new Handler();
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(HandlerActivity.this,ButtonActivity.class);
                    startActivity(intent);
                }
            },3000);
    

    2.handler线程间的通信

     mHandler = new Handler(){
                @Override
                public void handleMessage(@NonNull Message msg) {
                    super.handleMessage(msg);
                    switch (msg.what){
                        case 1:
                            ToastUtil.showMsg(HandlerActivity.this,"线程通信成功");
                            break;
                    }
                }
            };
    //开启子线程
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    Message msg = new Message();
                    msg.what=1;
                    mHandler.sendMessage(msg);
                }
            }.start();
    

    相关文章

      网友评论

        本文标题:Handler

        本文链接:https://www.haomeiwen.com/subject/zsrajktx.html