Android中文API(128) —— HandlerThread

來源:互聯網
上載者:User

 

前言

  本章內容是android.os.HandlerThread,版本為Android 3.1 r1,翻譯來自"雪鷹工作室",再次感謝"雪鷹工作室"!歡迎你一起參與Android的中文翻譯,聯絡我over140@gmail.com。

 

聲明

  歡迎轉載,但請保留文章原始出處:)

    部落格園:http://www.cnblogs.com/

    Android中文翻譯組:http://goo.gl/6vJQl

 

 

HandlerThread

譯者署名:雪鷹工作室

版本:Android 3.1 r1

 

結構

繼承關係

public class HandleThread extends Thread

        

java.lang.Object

java.lang.Thread

        android.os.HandlerThread

 

類概述

用於方便的建立一個含有looper的線程類。looper用來建立handler類。請注意,仍然需要調用start()方法。

 

建構函式

  public HandlerThread (String name)

  構造一個HandlerThread

 

  public HandlerThread (String name, int priority)

  構造一個HandlerThread

  參數

  priority 運行線程的優先順序,這個值是由Android.os.Process提供而不是來自java.lang.Thread.

 

公用方法

public Looper getLooper ()

這個方法返回與此線程相關的Looper。如果該線程不能啟動或是由於某種原因isAlive()方法返回false,則這個方法返回null,假如這個線程已經啟動,那麼此方法將阻塞,直到looper被初始化。

  傳回值

  Looper

 

public int getThreadId ()

返回線程的標誌。詳見:Process.myTid()

 

public boolean quit ()

查看當前啟動並執行looper是否已經退出。假如線程這未啟動或者已經結束(即getLooper()方法返回null),這個方法將返回false。否則looper被要求退出並返回true。

 

public void run ()

調用設定Runnable對象的run()方法。如果沒有設定Runnable,什麼也不做。

 

受保護方法

protected void onLooperPrepared ()

如果需要在Looper迴圈之前執行一些設定,可明確繼承該回調方法。

(譯者註:如下代碼:

         void run() {

                   mTid = Process.myTid();

                   Looper.prepare();//建立一個Looper執行個體,並且儲存在ThreadLocal中,ThreadLocal中維護一個HashMap,鍵是線程號

                   synchronized (this) {

                            mLooper = Looper.myLooper();//得到當前線程的Looper,就是剛才perpare方法中建立並儲存的那個Looper執行個體

                            Process.setThreadPriority(mPriority);

                            notifyAll();

                   }

         onLooperPrepared();

         Looper.loop();//開始輪詢

         mTid = -1;

 

補充

文章精選

             Android開發入門——handler使用(二)

             AsyncQueryHandler研究

             Thread和Looper以及Handler和Message詳解 Android開發必讀[Android123]

             android Thread HandlerThread Looper Message MessageQueue Handler的關係

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.