Android异步处理Handler

  • 什么是Handler
  • handler的使用方法
  • handler机制原理
  • handler引起的内存泄漏以及解决方法

什么是handler

handler用于主线程跟子线程之间的通信。比如更新UI操作一定要放在主线程(UI线程)中,但android里需及时响应用户的操作,所以一些耗时操作比如更新UI,下载回传等就应放在子线程中运行。矛盾由此产生了,又要主线程更新操作,但又不要把耗时操作放在主线程。那handler在这里起到一个连接的作用,即把操作放在子线程里,需要更新UI的时候,handler再告诉主线程要更新UI就可以了。

handler的使用

post(Runnable)

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_download;
    private TextView tv_show;

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_download = (Button)findViewById(R.id.button);
        tv_show = (TextView)findViewById(R.id.textView);
        btn_download.setOnClickListener(this);
    }
    //点击的这一下就是主线程中更新UI的操作了
    @Override
    public void onClick(View view) {
        DownLoadThread downLoadThread =new DownLoadThread();
        downLoadThread.start();

    }

//子线程更新UI
    class DownLoadThread extends Thread{
        @Override
        public void run() {

            try {
                System.out.println("开始下载···");
                Thread.sleep(5000);
                System.out.println("下载完成");
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        MainActivity.this.tv_show.setText("文件下载完成");
                    }
                };
                //在此用handler的.post(runnable)传递到主线程中使其更新
                handler.post(runnable);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

sendMessage(message)


public class MyHandlerSendMessage extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_show = null;

    private Button bn_download;

    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    MyHandlerSendMessage.this.tv_show.setText("文件下载完成");
                    break;
            }
        }
    };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sendmsg);
        tv_show = (TextView) findViewById(R.id.textView);
        bn_download = (Button) findViewById(R.id.button);
        bn_download.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        DownLoadThread downLoadThread= new DownLoadThread();
        downLoadThread.start();

    }

    class DownLoadThread extends Thread{
        @Override
        public void run() {

            try {
                System.out.println("开始下载···");
                Thread.sleep(3000);
                System.out.println("下载完成");
                Message msg = new Message();
                msg.what =1;
                handler.sendMessage(msg);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值