亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android后臺線程和UI線程通訊實例

 更新時間:2014年06月26日 09:05:36   作者:冰凍魚  
這篇文章主要介紹了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對象。如:

復(fù)制代碼 代碼如下:

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對象都會收到相同的消息。

復(fù)制代碼 代碼如下:

        /*
         * 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。

復(fù)制代碼 代碼如下:

// 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實例傳送。

復(fù)制代碼 代碼如下:

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。

復(fù)制代碼 代碼如下:

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)文章

最新評論