国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android后臺(tái)線程和UI線程通訊實(shí)例

2020-04-11 11:49:33
字體:
供稿:網(wǎng)友

本節(jié)向你展示如何在任務(wù)中發(fā)送數(shù)據(jù)給UI線程里的對(duì)象,這個(gè)特性允許你在后臺(tái)線程工作,完了在UI線程展示結(jié)果。

在UI線程定義一個(gè)Handler

Handler是Android系統(tǒng)線程管理框架里的一部分。一個(gè)Handler對(duì)象接收消息,并且運(yùn)行代碼來處理消息。正常情況下,你為新線程創(chuàng)建Handler,但你也可以為已有的線程創(chuàng)建一個(gè)Handler.當(dāng)你連接Handler到UI線程時(shí),處理消息的代碼會(huì)在UI線程上運(yùn)行.

在創(chuàng)建線程池的類的構(gòu)造器里實(shí)例化Handler對(duì)象,保存在全局變量里。用Handler(Looper)方法實(shí)例化,連接到UI線程,構(gòu)造方法使用Looper對(duì)象,也是Android系統(tǒng)線程管理框架里的一部分.Looper類有一個(gè)靜態(tài)方法getMainLooper()可以獲取UI線程的Looper對(duì)象。如:

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

private PhotoManager() {
...
    // Defines a Handler object that's attached to the UI thread
    mHandler = new Handler(Looper.getMainLooper()) {
    ...

在Handler里,覆蓋handleMessage()。Android系統(tǒng)會(huì)在Handler管理的線程收到新消息時(shí),調(diào)用該方法。一個(gè)指定線程的所有Handler對(duì)象都會(huì)收到相同的消息。

復(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ù)里移動(dòng)數(shù)據(jù)到UI線程

要從后臺(tái)線程的任務(wù)里移動(dòng)數(shù)據(jù)到UI線程的對(duì)象,先保存引用到數(shù)據(jù)和任務(wù)對(duì)象的UI對(duì)象里,接下來把任務(wù)對(duì)象和狀態(tài)碼傳給Handler對(duì)象。在這個(gè)對(duì)象里,發(fā)送一個(gè)包含狀態(tài) 和任務(wù)對(duì)象的消息給Handler.因?yàn)镠andler在UI線程上運(yùn)行,它可以移動(dòng)數(shù)據(jù)給UI對(duì)象。

在任務(wù)對(duì)象里存儲(chǔ)數(shù)據(jù)

如,這是一個(gè)Runnable,運(yùn)行在后臺(tái)線程,它解析Bitmap,并保存到它的父對(duì)象。Runnable同時(shí)保存狀態(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還包含一個(gè)ImageView引用,用來顯示Bitmap.盡管引用Bitmap和ImageView是在同一個(gè)對(duì)象里,但因?yàn)椴皇窃赨I線程,你不能直接讓ImageView顯示Bitmap.

沿對(duì)象層次逐級(jí)發(fā)送狀態(tài)

PhotoTask持有解碼的數(shù)據(jù)和顯示數(shù)據(jù)的View對(duì)象的引用,它從PhotoDecodeRunnable接收到狀態(tài)碼,并且沿著線程池里引用的對(duì)象和Handler實(shí)例傳送。

復(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);
    }
    ...
}

移動(dòng)數(shù)據(jù)到UI

PhotoManager從PhotoTask對(duì)象接收到狀態(tài)碼和PhotoTask對(duì)象的句柄。因?yàn)闋顟B(tài)是TASK_COMPLETE,創(chuàng)建一個(gè)包含狀態(tài)和任務(wù)對(duì)象的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()為每個(gè)進(jìn)來的Message檢查狀態(tài)碼。如果狀態(tài)碼是TASK_COMPLETE,任務(wù)就是完成了,Message里的PhotoTask對(duì)象包含Bitmap和ImageView.因?yàn)镠andler.handleMessage()運(yùn)行在UI線程,它可以安全地為ImageView設(shè)置Bitmap.

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 芮城县| 怀来县| 改则县| 峡江县| 惠东县| 荔浦县| 淳安县| 紫云| 博罗县| 永春县| 军事| 司法| 金溪县| 磐石市| 金阳县| 上杭县| 余姚市| 宜君县| 金门县| 淮北市| 翁源县| 夏津县| 宾川县| 和政县| 当阳市| 北碚区| 武乡县| 招远市| 长岛县| 临夏市| 扶余县| 西贡区| 图们市| 莱阳市| 米泉市| 繁昌县| 朝阳县| 白城市| 独山县| 江永县| 胶州市|