Android后臺線程和UI線程通訊實例
本節(jié)向你展示如何在任務(wù)中發(fā)送數(shù)據(jù)給UI線程里的對象,這個特性允許你在后臺線程工作,完了在UI線程展示結(jié)果。
在UI線程定義一個Handler
Handler是Android系統(tǒng)線程管理框架里的一部分。一個Handler對象接收消息,并且運行代碼來處理消息。正常情況下,你為新線程創(chuàng)建Handler,但你也可以為已有的線程創(chuàng)建一個Handler.當(dāng)你連接Handler到UI線程時,處理消息的代碼會在UI線程上運行.
在創(chuàng)建線程池的類的構(gòu)造器里實例化Handler對象,保存在全局變量里。用Handler(Looper)方法實例化,連接到UI線程,構(gòu)造方法使用Looper對象,也是Android系統(tǒng)線程管理框架里的一部分.Looper類有一個靜態(tài)方法getMainLooper()可以獲取UI線程的Looper對象。如:
private PhotoManager() {
...
// Defines a Handler object that's attached to the UI thread
mHandler = new Handler(Looper.getMainLooper()) {
...
在Handler里,覆蓋handleMessage()。Android系統(tǒng)會在Handler管理的線程收到新消息時,調(diào)用該方法。一個指定線程的所有Handler對象都會收到相同的消息。
/*
* handleMessage() defines the operations to perform when
* the Handler receives a new Message to process.
*/
@Override
public void handleMessage(Message inputMessage) {
// Gets the image task from the incoming Message object.
PhotoTask photoTask = (PhotoTask) inputMessage.obj;
...
}
...
}
}
從任務(wù)里移動數(shù)據(jù)到UI線程
要從后臺線程的任務(wù)里移動數(shù)據(jù)到UI線程的對象,先保存引用到數(shù)據(jù)和任務(wù)對象的UI對象里,接下來把任務(wù)對象和狀態(tài)碼傳給Handler對象。在這個對象里,發(fā)送一個包含狀態(tài) 和任務(wù)對象的消息給Handler.因為Handler在UI線程上運行,它可以移動數(shù)據(jù)給UI對象。
在任務(wù)對象里存儲數(shù)據(jù)
如,這是一個Runnable,運行在后臺線程,它解析Bitmap,并保存到它的父對象。Runnable同時保存狀態(tài)碼DECODE_STATE_COMPLETED。
// A class that decodes photo files into Bitmaps
class PhotoDecodeRunnable implements Runnable {
...
PhotoDecodeRunnable(PhotoTask downloadTask) {
mPhotoTask = downloadTask;
}
...
// Gets the downloaded byte array
byte[] imageBuffer = mPhotoTask.getByteBuffer();
...
// Runs the code for this task
public void run() {
...
// Tries to decode the image buffer
returnBitmap = BitmapFactory.decodeByteArray(
imageBuffer,
0,
imageBuffer.length,
bitmapOptions
);
...
// Sets the ImageView Bitmap
mPhotoTask.setImage(returnBitmap);
// Reports a status of "completed"
mPhotoTask.handleDecodeState(DECODE_STATE_COMPLETED);
...
}
...
}
...
PhotoTask還包含一個ImageView引用,用來顯示Bitmap.盡管引用Bitmap和ImageView是在同一個對象里,但因為不是在UI線程,你不能直接讓ImageView顯示Bitmap.
沿對象層次逐級發(fā)送狀態(tài)
PhotoTask持有解碼的數(shù)據(jù)和顯示數(shù)據(jù)的View對象的引用,它從PhotoDecodeRunnable接收到狀態(tài)碼,并且沿著線程池里引用的對象和Handler實例傳送。
public class PhotoTask {
...
// Gets a handle to the object that creates the thread pools
sPhotoManager = PhotoManager.getInstance();
...
public void handleDecodeState(int state) {
int outState;
// Converts the decode state to the overall state.
switch(state) {
case PhotoDecodeRunnable.DECODE_STATE_COMPLETED:
outState = PhotoManager.TASK_COMPLETE;
break;
...
}
...
// Calls the generalized state method
handleState(outState);
}
...
// Passes the state to PhotoManager
void handleState(int state) {
/*
* Passes a handle to this task and the
* current state to the class that created
* the thread pools
*/
sPhotoManager.handleState(this, state);
}
...
}
移動數(shù)據(jù)到UI
PhotoManager從PhotoTask對象接收到狀態(tài)碼和PhotoTask對象的句柄。因為狀態(tài)是TASK_COMPLETE,創(chuàng)建一個包含狀態(tài)和任務(wù)對象的Message,發(fā)送給Handler。
public class PhotoManager {
...
// Handle status messages from tasks
public void handleState(PhotoTask photoTask, int state) {
switch (state) {
...
// The task finished downloading and decoding the image
case TASK_COMPLETE:
/*
* Creates a message for the Handler
* with the state and the task object
*/
Message completeMessage =
mHandler.obtainMessage(state, photoTask);
completeMessage.sendToTarget();
break;
...
}
...
}
最終,Handler.handleMessage()為每個進(jìn)來的Message檢查狀態(tài)碼。如果狀態(tài)碼是TASK_COMPLETE,任務(wù)就是完成了,Message里的PhotoTask對象包含Bitmap和ImageView.因為Handler.handleMessage()運行在UI線程,它可以安全地為ImageView設(shè)置Bitmap.
相關(guān)文章
淺析Android手機(jī)衛(wèi)士之手機(jī)實現(xiàn)短信指令獲取位置
這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士之手機(jī)實現(xiàn)短信指令獲取位置的相關(guān)資料,需要的朋友可以參考下2016-04-04Flow轉(zhuǎn)LiveData數(shù)據(jù)丟失原理詳解
這篇文章主要為大家介紹了Flow轉(zhuǎn)LiveData數(shù)據(jù)丟失原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android中button實現(xiàn)onclicklistener事件的兩種方式
本文介紹下Android中button實現(xiàn)onclicklistener事件的兩種方法,感興趣的朋友可以參考下2013-04-04Android中Fragment的加載方式與數(shù)據(jù)通信詳解
本文主要介紹了Android中Fragment的加載方式與數(shù)據(jù)通信的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03Flutter定義tabbar底部導(dǎo)航路由跳轉(zhuǎn)的方法
這篇文章主要為大家詳細(xì)介紹了Flutter定義tabbar底部導(dǎo)航路由跳轉(zhuǎn)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07