Android -- Looper.prepare()和Looper.loop() —深度版
Android中的Looper類,是用來封裝消息循環(huán)和消息隊(duì)列的一個(gè)類,用于在android線程中進(jìn)行消息處理。handler事實(shí)上能夠看做是一個(gè)工具類。用來向消息隊(duì)列中插入消息的。
? ? (1) Looper類用來為一個(gè)線程開啟一個(gè)消息循環(huán)。
??? 默認(rèn)情況下android中新誕生的線程是沒有開啟消息循環(huán)的。(主線程除外,主線程系統(tǒng)會(huì)自己主動(dòng)為其創(chuàng)建Looper對(duì)象,開啟消息循環(huán)。) ??? Looper對(duì)象通過MessageQueue來存放消息和事件。
一個(gè)線程僅僅能有一個(gè)Looper。相應(yīng)一個(gè)MessageQueue。?
(2) 一般是通過Handler對(duì)象來與Looper進(jìn)行交互的。
Handler可看做是Looper的一個(gè)接口。用來向指定的Looper發(fā)送消息及定義處理方法。 ??? 默認(rèn)情況下Handler會(huì)與其被定義時(shí)所在線程的Looper綁定,比方,Handler在主線程中定義。那么它是與主線程的Looper綁定。
mainHandler = new Handler() 等價(jià)于new Handler(Looper.myLooper()). Looper.myLooper():獲取當(dāng)前進(jìn)程的looper對(duì)象,類似的 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)沒有創(chuàng)建Looper對(duì)象,須要先調(diào)用Looper.prepare()啟用Looper。?
(4) Looper.loop(); 讓Looper開始工作,從消息隊(duì)列里取消息,處理消息。?
(5) 基于以上知識(shí),可實(shí)現(xiàn)主線程給子線程(非主線程)發(fā)送消息。?
??? 把以下樣例中的mHandler聲明成類成員,在主線程通過mHandler發(fā)送消息就可以。
??? ??? 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 of prepare() 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(); }
假設(shè)線程中使用Looper.prepare()和Looper.loop()創(chuàng)建了消息隊(duì)列就能夠讓消息處理在該線程中完畢。
?android HandlerThread使用小例
之前研究過handler 和 looper 消息隊(duì)列。只是android里的handler不是另外開啟線程來運(yùn)行的。還是在主UI線程中,假設(shè)想另啟線程的話須要用到HandlerThread來實(shí)現(xiàn)。在使用HandlerThread的時(shí)候須要實(shí)現(xiàn)CallBack接口以重寫handlerMessage方法。在handlerMessage方法中來處理自己的邏輯。
下來給出一個(gè)小樣例程序。
layout文件非常easy,就一個(gè)button來啟動(dòng)HanlderTread線程
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/handlerThreadBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="startHandlerThread" /> </LinearLayout>
Activity代碼例如以下:
package com.tayue;import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Handler.Callback; import android.os.HandlerThread; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /*** * @author xionglei**/ public class TestHandlerActivity extends Activity implements OnClickListener{public Button handlerThreadBTN; MyHandlerThread handlerThread;Handler handler;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//打印UI線程的名稱System.out.println("onCreate CurrentThread = " + Thread.currentThread().getName());setContentView(R.layout.main);handlerThreadBTN = (Button) findViewById(R.id.handlerThreadBtn);handlerThreadBTN.setOnClickListener(this);handlerThread = new MyHandlerThread("myHanler");handlerThread.start();//注意: 這里必須用到handler的這個(gè)構(gòu)造器,由于須要把callback傳進(jìn)去,從而使自己的HandlerThread的handlerMessage來替換掉Handler原生的handlerThreadhandler = new Handler(handlerThread.getLooper(), handlerThread); }@Overridepublic void onClick(View v) {//點(diǎn)擊button后來開啟線程handler.sendEmptyMessage(1);} private class MyHandlerThread extends HandlerThread implements Callback {public MyHandlerThread(String name) {super(name);}@Overridepublic boolean handleMessage(Message msg) {//打印線程的名稱System.out.println(" handleMessage CurrentThread = " + Thread.currentThread().getName());return true;} } }
點(diǎn)擊button。打印的日志例如以下(這里點(diǎn)擊了3次) 07-06 09:32:48.776: I/System.out(780): onCreate? CurrentThread = main 07-06 09:32:55.076: I/System.out(780):? handleMessage CurrentThread = myHanler 07-06 09:32:58.669: I/System.out(780):? handleMessage CurrentThread = myHanler 07-06 09:33:03.476: I/System.out(780):? handleMessage CurrentThread = myHanler
HandlerThread就這么簡(jiǎn)單。
當(dāng)然 android自己也有異步線程的handler,就是AsyncTask。這個(gè)類就是封裝了HandlerThread 和handler來實(shí)現(xiàn)異步多線程的操作的。
相同能夠這樣使用:
private boolean iscancel = false; //用戶手動(dòng)取消登錄的標(biāo)志位handlerThread = new HandlerThread("myHandlerThread");handlerThread.start();handler = new MyHandler(handlerThread.getLooper());// 將要運(yùn)行的線程對(duì)象加入到線程隊(duì)列中handler.post(new Runnable() {@Overridepublic void run() {Message message = handler.obtainMessage();UserBean user = Bbs.getInstance().Login(username, password);//耗時(shí)任務(wù)Bundle b = new Bundle();b.putSerializable("user", user);message.setData(b);message.sendToTarget(); //或使用 handler.sendMessage(message);}});class MyHandler extends Handler {public MyHandler(Looper looper) {super(looper);}@Overridepublic void handleMessage(Message msg) {if(iscancel == false){// 操作UI線程的代碼Bundle b = msg.getData();UserBean user = (UserBean)b.get("user");......}}}轉(zhuǎn)載于:https://www.cnblogs.com/blfshiye/p/4590416.html
總結(jié)
以上是生活随笔為你收集整理的Android -- Looper.prepare()和Looper.loop() —深度版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rpk文件怎么打开
- 下一篇: android sina oauth2.