Android開發之Handler和Looper的關係

來源:互聯網
上載者:User

標籤:android   handler   looper   thread   queue   

          關於Handler的總結。

Message:訊息,其中包含了訊息ID,訊息處理對象以及處理的資料等,由MessageQueue統一列隊,終由Handler處理。

Handler:處理者,負責Message的發送及處理。使用Handler時,需要實現handleMessage(Message msg)方法來對特定的Message進行處理,例如更新UI等。

MessageQueue:訊息佇列,用來存放Handler發送過來的訊息,並按照FIFO規則執行。當然,存放Message並非實際意義的儲存,而是將Message以鏈表的方式串聯起來的,等待Looper的抽取。

Looper:訊息泵,不斷地從MessageQueue中抽取Message執行。因此,一個MessageQueue需要一個Looper。

Thread:線程,負責調度整個訊息迴圈,即訊息迴圈的執行場所。

    ,Handler負責將資料壓棧到Message隊尾中,並從隊頭取出資料,而Looper負責從隊列中進行迴圈取出,得到Handler的資料。注意Looper必須是UI的Looper,我們才能對UI介面進行更新!


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.