常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          Android Looper(轉(zhuǎn))

          (1) Looper類別用來(lái)為一個(gè)線程開(kāi)啟一個(gè)消息循環(huán)。默認(rèn)情況下Android中新誕生的線程是沒(méi)有開(kāi)啟消息循環(huán)的。(主線程除外,主線程系統(tǒng)會(huì)自動(dòng)為其創(chuàng)建Looper對(duì)象,開(kāi)啟消息循環(huán))

          Looper對(duì)象通過(guò)MessageQueue來(lái)存放消息和事件。一個(gè)線程只能有一個(gè)Looper,對(duì)應(yīng)一個(gè)MessageQueue。

          (2) 通常是通過(guò)Handler對(duì)象來(lái)與Looper交互的。Handler可看做是Looper的一個(gè)接口,用來(lái)向指定的Looper發(fā)送消息及定義處理方法。

          默認(rèn)情況下Handler會(huì)與其被定義時(shí)所在線程的Looper綁定,比如,在主線程中定義,其是與主線程的Looper綁定。

          mainHandler = new Handler() 等價(jià)于new Handler(Looper.myLooper()).

          Looper.myLooper():Return the Looper object associated with the current thread 獲取當(dāng)前進(jìn)程的looper對(duì)象。

          還有一個(gè)類似的 Looper.getMainLooper() 用于獲取主線程的Looper對(duì)象。

          (3) 在非主線程中直接new Handler() 會(huì)報(bào)如下的錯(cuò)誤:

          E/AndroidRuntime( 6173): Uncaught handler: thread Thread-8 exiting due to uncaught exception
          E/AndroidRuntime( 6173): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

          原因是非主線程中默認(rèn)沒(méi)有創(chuàng)建Looper對(duì)象,需要先調(diào)用Looper.prepare()啟用Looper。

          (4) Looper.loop(); 讓Looper開(kāi)始工作,從消息隊(duì)列里取消息,處理消息。

          注意:寫在Looper.loop()之后的代碼不會(huì)被執(zhí)行,這個(gè)函數(shù)內(nèi)部應(yīng)該是一個(gè)循環(huán),當(dāng)調(diào)用mHandler.getLooper().quit()后,loop才會(huì)中止,其后的代碼才能得以運(yùn)行。

          (5) 基于以上知識(shí),可實(shí)現(xiàn)主線程給子線程(非主線程)發(fā)送消息。

          把下面例子中的mHandler聲明成類成員,在主線程通過(guò)mHandler發(fā)送消息即可。

          (6) Android官方文檔中Looper的介紹:

          Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.

          Most interaction with a message loop is through the Handler class.

          This is a typical example of the implementation of a Looper thread, using the separation ofprepare() and loop() to create an initial Handler to communicate with the Looper.

          class LooperThread extends Thread {
                public Handler mHandler;
                
                public void run() {
                    Looper.prepare();
                    
                    mHandler = new Handler() {
                        public void handleMessage(Message msg) {
                            // process incoming messages here
                        }
                    };
                    
                    Looper.loop();
                }
          }

          posted on 2010-12-20 15:13 九寶 閱讀(401) 評(píng)論(0)  編輯  收藏 所屬分類: android

          主站蜘蛛池模板: 阜新市| 英山县| 昌江| 磐石市| 平南县| 湘潭市| 岳阳市| 乳山市| 城口县| 安义县| 新营市| 清丰县| 日土县| 集贤县| 苏州市| 将乐县| 嘉鱼县| 甘泉县| 定襄县| 奉化市| 辰溪县| 南岸区| 施秉县| 西和县| 琼中| 前郭尔| 神木县| 邢台市| 新竹市| 泸西县| 潜江市| 抚远县| 乌苏市| 威宁| 开封市| 海晏县| 宜昌市| 洛浦县| 宜宾县| 玉山县| 安顺市|