Android中線程之(zhi)間的通信(xin)
時間:2018-09-27 來源:未知
andriod提供了(le) Handler 和 Looper 來(lai)滿(man)足線程(cheng)間的(de)通信。例如一個(ge)(ge)子線程(cheng)從網絡上下載了(le)一副圖片,當(dang)它(ta)下載完成后會發送消息給主線程(cheng),這個(ge)(ge)消息是(shi)通過(guo)綁(bang)定(ding)在(zai)主線程(cheng)的(de)Handler來(lai)傳遞的(de)。
本文引用地址://fsbing.cn/emb/Column/7469.html
在Android,這(zhe)(zhe)里(li)(li)的(de)(de)(de)(de)線(xian)(xian)(xian)(xian)(xian)程(cheng)(cheng)(cheng)分為有(you)(you)消(xiao)(xiao)息(xi)(xi)循(xun)環(huan)(huan)的(de)(de)(de)(de)線(xian)(xian)(xian)(xian)(xian)程(cheng)(cheng)(cheng)和沒有(you)(you)消(xiao)(xiao)息(xi)(xi)循(xun)環(huan)(huan)的(de)(de)(de)(de)線(xian)(xian)(xian)(xian)(xian)程(cheng)(cheng)(cheng),有(you)(you)消(xiao)(xiao)息(xi)(xi)循(xun)環(huan)(huan)的(de)(de)(de)(de)線(xian)(xian)(xian)(xian)(xian)程(cheng)(cheng)(cheng)一(yi)(yi)(yi)(yi)般都會有(you)(you)一(yi)(yi)(yi)(yi)個(ge)Looper,這(zhe)(zhe)個(ge)是(shi)(shi)android的(de)(de)(de)(de)新(xin)概念。我(wo)(wo)(wo)們的(de)(de)(de)(de)主線(xian)(xian)(xian)(xian)(xian)程(cheng)(cheng)(cheng)(UI線(xian)(xian)(xian)(xian)(xian)程(cheng)(cheng)(cheng))就(jiu)(jiu)是(shi)(shi)一(yi)(yi)(yi)(yi)個(ge)消(xiao)(xiao)息(xi)(xi)循(xun)環(huan)(huan)的(de)(de)(de)(de)線(xian)(xian)(xian)(xian)(xian)程(cheng)(cheng)(cheng)。針(zhen)對這(zhe)(zhe)種消(xiao)(xiao)息(xi)(xi)循(xun)環(huan)(huan)的(de)(de)(de)(de)機制,我(wo)(wo)(wo)們引入(ru)一(yi)(yi)(yi)(yi)個(ge)新(xin)的(de)(de)(de)(de)機制Handler,我(wo)(wo)(wo)們有(you)(you)消(xiao)(xiao)息(xi)(xi)循(xun)環(huan)(huan),就(jiu)(jiu)要(yao)(yao)往消(xiao)(xiao)息(xi)(xi)循(xun)環(huan)(huan)里(li)(li)面(mian)發送相(xiang)應(ying)(ying)的(de)(de)(de)(de)消(xiao)(xiao)息(xi)(xi),自(zi)定義消(xiao)(xiao)息(xi)(xi)一(yi)(yi)(yi)(yi)般都會有(you)(you)自(zi)己對應(ying)(ying)的(de)(de)(de)(de)處(chu)理,消(xiao)(xiao)息(xi)(xi)的(de)(de)(de)(de)發送和清除,把這(zhe)(zhe)些(xie)都封(feng)裝在Handler里(li)(li)面(mian),注意Handler只是(shi)(shi)針(zhen)對那 些(xie)有(you)(you)Looper的(de)(de)(de)(de)線(xian)(xian)(xian)(xian)(xian)程(cheng)(cheng)(cheng),不(bu)管(guan)是(shi)(shi)UI線(xian)(xian)(xian)(xian)(xian)程(cheng)(cheng)(cheng)還是(shi)(shi)子線(xian)(xian)(xian)(xian)(xian)程(cheng)(cheng)(cheng),只要(yao)(yao)你有(you)(you)Looper,我(wo)(wo)(wo)就(jiu)(jiu)可以往你的(de)(de)(de)(de)消(xiao)(xiao)息(xi)(xi)隊列(lie)里(li)(li)面(mian)添加東西,并做(zuo)相(xiang)應(ying)(ying)的(de)(de)(de)(de)處(chu)理。但是(shi)(shi)這(zhe)(zhe)里(li)(li)還有(you)(you)一(yi)(yi)(yi)(yi)點,就(jiu)(jiu)是(shi)(shi)只要(yao)(yao)是(shi)(shi)關于UI相(xiang)關的(de)(de)(de)(de)東西,就(jiu)(jiu)不(bu)能放在子線(xian)(xian)(xian)(xian)(xian)程(cheng)(cheng)(cheng)中,因(yin)為子線(xian)(xian)(xian)(xian)(xian)程(cheng)(cheng)(cheng)是(shi)(shi)不(bu)能操(cao)作UI的(de)(de)(de)(de),只能進行(xing)數據、系(xi)統等其他非UI的(de)(de)(de)(de)操(cao)作。
一個Handler的(de)創(chuang)(chuang)建它就會被(bei)綁(bang)定到這個線(xian)(xian)程的(de)消息(xi)隊(dui)列中,如果是(shi)在主(zhu)線(xian)(xian)程創(chuang)(chuang)建的(de),那就不(bu)需(xu)要寫代碼(ma)來創(chuang)(chuang)建消息(xi)隊(dui)列了,默認的(de)消息(xi)隊(dui)列會在主(zhu)線(xian)(xian)程被(bei)創(chuang)(chuang)建。但是(shi)如果是(shi)在子線(xian)(xian)程的(de)話,就必須在創(chuang)(chuang)建Handler之前先初始化線(xian)(xian)程的(de)消息(xi)隊(dui)列。請看如下代碼(ma):
class ChildThread extends Thread {
public void run() {
/*
* 創(chuang)建 handler前先初始化Looper.
*/
Looper.prepare();
/*
* 在子線程創建handler,所以會(hui)綁定到(dao)子線程的消息隊列中
*
*/
mChildHandler = new Handler() {
public void handleMessage(Message msg) {
/*
* Do some expensive operations there.
*/
}
};
/*
* 啟動(dong)該線程的消(xiao)息(xi)隊(dui)列(lie)
*/
Looper.loop();
}
}
當(dang)Handler收到消息后,就會運行執行handlerMessage()方法,可以在(zai)里面(mian)獲取另外一個線(xian)程(cheng)發送過來的(de)Message對(dui)象,到達線(xian)程(cheng)之間通信的(de)過程(cheng)。
后完成(cheng)了操(cao)作要結束子(zi)線(xian)程時,記得調用(yong)quit()來結束消息(xi)循(xun)環隊列;
mChildHandler.getLooper().quit();

