与Handler有关的几个概念

与Handler有关的几个概念

注: 相关图片都是用的别人的

1.Handler 主要用于消息处理

2.Message:消息,子线程向UI线程发送消息,消息中携带者相应数据。

3.MessageQueue:消息队列,有一个格Message消息组成

4.Looper :信息泵,循环处理消息队列MessageQueue中的Message,将其发送给相应的Handler

5.LooperThread,如果要非UI线程中实例化Handler,必须有Looper,而一般子线程中是不存在Looper的,一个线程可以有一个Looper对象和一个MessageQueue,必行显示调用Looper.loop( )方法给其分配出Looper对象才能使用。

一个线程对应一个Looper

一个Looper对应一个MessageQueue

一个Looper可以对应多个Handler

记住:HandlerThread =普通Thread+Looper

详细的运行过程看下两图:

 

与Handler有关的几个概念

 

与Handler有关的几个概念

一.Handler的方法使用

Handler可以将两种类型消息放到消息队列中,一种是Message另种是Runnable对象,如果是Message对象时,通过HandlerhandleMessage( )方法处理,如果是Runnable对象的时候,当Handler拿到Looper发送过来的Runnable对象的时候,会直接运行Runnablerun( )方法,注意Runnable对象时运行在相应Handler所在的线程中的(一般是UI线程),没有start,直接调用了run( )方法。

 

  1. /** 
  2.                  *  
  3.                  * handler.post(Runnable); 
  4.                  *  
  5.                  *  
  6.                  */ 
  7.                 handler.post(new Runnable() { 
  8.  
  9.                     @Override 
  10.                     public void run() { 
  11.                         isChanging = true
  12.                         bar.setProgress(count); 
  13.                         count++; 
  14.  
  15.                     } 
  16.  
  17.  
  18.                 }); 
  19.  
  20.  
  21.                 /** 
  22.                  *  
  23.                  *  
  24.                  * hanlder.postDelayer(Runnable,timeDelayed); 
  25.                  *  
  26.                  */ 
  27.                 handler.postDelayed(new Runnable() { 
  28.  
  29.                     @Override 
  30.                     public void run() { 
  31.                         isChanging = true
  32.                         bar.setProgress(count); 
  33.                         count++; 
  34.  
  35.                     } 
  36.  
  37.                 },100); 
  38. /** 
  39.  * 重点看一下handler是怎么发送msg的 
  40.  *  
  41.  * 1.可以发送空的Message 
  42.  * 2.可以定时发送Message 
  43.  * 3.Message可以携带的数据有   int what,int arg1,int arg2,Object obj, Bundle bundler(通过msg.setData(Bundle bundler)来添加到Message对象中) 
  44.  *  
  45.  * 4.Message通过Handler.obtain()方法实例化,可以进行有参、无参实例化,其中有参实例化是可以出传入 int what,int arg1,int arg2, Object obj 
  46.  * @author VicentTung 
  47.  * 
  48.  */ 
  49.  
  50.  
  51.  
  52.  
  53.                 /** 
  54.                  * 1.发送空的Message 
  55.                  */ 
  56.  
  57.                 handler.sendEmptyMessage(0); 
  58.  
  59.                 handler.sendEmptyMessageAtTime(0, System.currentTimeMillis()); 
  60.  
  61.                 handler.sendEmptyMessageDelayed(0, 100); 
  62.  
  63.  
  64.                 /** 
  65.                  *2. 发送非空Message(Message是new出来的) 
  66.                  */ 
  67.                 Message msg =new Message(); 
  68.                 Bundle data_bundle = new Bundle(); 
  69.                 data_bundle.putString("name", "小明"); 
  70.                 data_bundle.putInt("age", 12); 
  71.                 msg.arg1=1; 
  72.                 msg.arg2=2; 
  73.                 msg.what=3
  74.                 msg.obj="success"
  75.                 msg.setData(data_bundle); 
  76.                 handler.sendMessageDelayed(msg, 100); 
  77.  
  78.                 /** 
  79.                  * 3.发送非空Message(Message是handler.obtain()出来的) 
  80.                  */ 
  81.  
  82.                  
  83.                 String obj="hahah"
  84.                 int arg1=111
  85.                 int arg2=222
  86.                 Message msg_obtain =handler.obtainMessage(); 
  87.              
  88.                 /** 
  89.                  *  各种各样的带参数的obtain()方法实例化出Message对象 
  90.                  */ 
  91.                 msg_obtain =handler.obtainMessage(what); 
  92.                 msg_obtain = handler.obtainMessage(what, obj); 
  93.                 msg_obtain = handler.obtainMessage(what, arg1, arg2); 
  94.                 msg_obtain =handler.obtainMessage(what, arg1, arg2, obj); 
  95.  
  96.                  
  97.                 /** 
  98.                  * 4.Message的移除 
  99.                  */ 
  100.  
  101.                  
  102.                 if(handler.hasMessages(0)){ 
  103.                      
  104.                     handler.removeMessages(0); 
  105.                 }