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

猜你喜欢

转载自blog.csdn.net/qq_34591917/article/details/80071498
今日推荐