Android 中handler和handlerThread的使用

handler的使用

handler的两个作用

可以安排message或者runnable对象在将来的某个时间点执行
可以将一个action让其他线程执行而不是自己的线程

You can create your own threads, and communicate back with the main application thread through a Handler

利用hanlder可以在自己创建的新线程和主线程直间交互,例如创建一个下载文件的线程,然后根据下载的进度更新主UI中的显示,由于在android中不能用自己创建的线程来更新UI界面,这时候可以利用主线程的handler,handler.sendMessage(msg)这样msg就会传入主线程的消息队列,主线程根据msg对象所封装的信息

执行相应的UI更新操作,这样就实现了其他线程和主线程之间的数据交互。



handler可以允许你发送和操作一个Message对象或者Runnable对象,每一个handler都会绑定到一个线程和该线程的消息队列,
当创建一个新的handler时,这个handler就被绑定到创建该handler的线程和该线程的消息队列。从那一时刻起,这个handler会将Message对象和Runnable对象发送到绑定的消息队列中并且负责执行他们在他们从消息队列中弹出的时候。
handler的两个作用:1、安排Message和Runnable对象的定时执行 2、在新的线程中执行操作


Scheduling messages is accomplished with the post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods. 
post的参数是Runnable对象,sedMessage的参数是Message对象
sendMessage对象从队列中弹出时,这个handler中的handleMessage负责执行该消息。
当一个程序启动的时候,主线程是专门用来running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create
,我们可以创建你自己的线程,通过handler和主线程进行交互。
Handler handler = new Handler(),这样创建的handler对象,当执行
handler.sendMessage(Message msg)或者handler.post(Runnable thread)时会将消息发送到主线程的消息队列,并用主线程执行,如果是Message对象,将会执行hanlder中的handleMessage方法,如果是Runnable对象,那么该Runnable对象的run方法将会被执行。

HandlerThread继承于Thread,所以它本质就是个Thread。与普通Thread的差别就在于,它有个Looper成员变量。这个Looper其实就是对消息队列以及队列处理逻辑的封装

下面这种方式创建的handler对象,当执行handler.sendMessage(Message msg)或者handler.post(Runnable thread)时会将消息或者线程发送到handlerThread这个线程中而不是主线程,息队列也是由该线程自己执行而不会影响到主线程的执行,这样就防止了主UI线程能够及时响应用户的操作,防止了ANR错误
HandlerThread handlerThread = new HandlerThread("handlerThread");
handlerThread.start();
handler = new MyHandler(handlerThread.getLooper());


MyHandler的声明
class MyHandler extends Handler
    {
        //空的构造函数
        public MyHandler()
        {}
        //以Looper类型参数传递的函数,Looper为消息泵,不断循环的从消息队列中得到消息并处理,因此
        //每个消息队列都有一个Looper,因为Looper是已经封装好了的消息队列和消息循环的类
        public MyHandler(Looper looper)
        {
        super(looper);
        }
        @Override
        public void handleMessage(Message msg) {
       
        progressBar.setProgress(cnt);
//progressTextView.setVisibility(View.VISIBLE);
//progressTextView.setText("当前进度" + cnt + "%");
       
            // TODO Auto-generated method stub
            System.out.println("Handler_ID---->"+Thread.currentThread().getId());
            System.out.println("Handler_Name---->"+Thread.currentThread().getId());
            //将消息中的bundle数据取出来
            Bundle b = msg.getData();
            String whether = b.getString("whether");
            int temperature = b.getInt("temperature");
            System.out.println("whether= "+whether+" ,temperature= "+temperature);
        }
       
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值