Android Message和obtainMessage的差別


參考:http://www.2cto.com/kf/201311/255885.html

http://www.cnblogs.com/over140/archive/2011/06/24/2088637.html

類概述

定義一個包括隨意類型的描寫敘述數據對象,此對象能夠發送給Handler

對象包括兩個額外的int字段和一個額外的對象字段。這樣能夠使得在非常多情況下不用做分配工作。

雖然Message的構造器是公開的。可是獲取Message對象的最好方法是調用Message.obtain()或者Handler.obtainMessage(), 這樣是從一個可回收對象池中獲取Message對象。


1、首先創建Handler對象:

[java]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
  1. private Handler mHandler = new Handler() {  
  2.   
  3.         public void handleMessage(android.os.Message msg) {  
  4.             switch (msg.what) {  
  5.             case 1:  
  6.                 textShowTV.setText("展示中...");  
  7.                 break;  
  8.             }  
  9.         };  
  10.           
  11.     };  

 

2、然后是消息處理:

[java]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
  1.             //①。使用new Message()  
  2. //          Message mess = new Message();  
  3.             //②,使用Message.obtain()  
  4.             Message mess = Message.obtain();  
  5.             mess.what =1;  
  6.             //mHandler.obtainMessage(1)與上兩行的代碼一樣,能夠參考源代碼查看  
  7. //          Message mess = mHandler.obtainMessage(1);  
  8.             mHandler.sendMessage(mess);  



通過比較我們會發現,這兩種獲取Message的實例的方法不一樣。於是我看了源代碼,果然不一樣:
 


進入obtain方法:

 
圖1:

 

進入Message方法:


圖2:

 
 

查看obtainMessage()源代碼:

 
圖3:

   

查看Message.obtain(this, what) 源代碼:

圖4:


然后,再次點擊obtain() 方法,代碼又回歸到了圖1

在handler.obtainMessage()的參數是這樣寫的:
Message android.os.Handler.obtainMessage(int what, int arg1, int arg2, Object obj)

public final Message obtainMessage (int what, int arg1, int arg2, Object obj)
Since: API Level 1
Same as obtainMessage(), except that it also sets the what, obj, arg1,and arg2 values on the returned Message.

Parameters
what  Value to assign to the returned Message.what field.
arg1  Value to assign to the returned Message.arg1 field.
arg2  Value to assign to the returned Message.arg2 field.
obj  Value to assign to the returned Message.obj field.

而Handler中obtainMessage與new Message的差別:

obtainmessage()是從消息池中拿來一個msg 不須要另開辟空間new

new須要又一次申請,效率低,obtianmessage能夠循環利用。

 //use Handler.obtainMessage(),instead of msg new Message();   

//because if there is already an Message object,that not be used by    

//any one ,the system will hand use that object,so you don't have to    

//create and object and allocate memory.   

//it  is also another example of object recycling and reusing in android.   

    Message msg mHandler.obtainMessage();  

    msg.what UPDATE_LISTVIEW;  

    msg.obj current "/" total "songs" 

    //this method is called from worker Thread,so we cannot update UI from here.  

    msg.sendToTarget();  

再看以下代碼:
Message msg handler.obtainMessage();  

   msg.arg1 i;  

   msg.sendToTarget();    

Message msg=new Message();  

   msg.arg1=i;  

   handler.sendMessage(msg);

 第一種寫法是message 從handler 類獲取,從而能夠直接向該handler 對象發送消息。另外一種寫法是直接調用 handler 的發送消息方法發送消息。

  

總結:

上面的圖1中obtain方法的凝視中說得非常明確:從整個Messge池中返回一個新的Message實例,在很多情況下使用它,由於它能避免分配新的對象
假設是這種話。那么通過調用obtainMessage方法獲取Message對象就能避免創建對象,從而降低內存的開銷了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM