Handler from entry to continue

Android asynchronous message mechanism --Handler

Android's Handler mechanism is an inter-thread communication mechanism, to avoid time-consuming operation resulting in Activity timeout and closed issue in the main thread.
At first look this mechanism, see a lot of Looper, MessageQueue, Handler, etc., mentioned the source code level, I feel really unfathomable.

Process Handler mechanism

After reading a lot blog, I discovered that in fact when entry not look so complicated, just enough to know Handler, Handler can be translated as handler, who is handling the message meant. A thread is equivalent to his secretary, when A has things to do entrusted to B, B to inform the secretary called, the secretary told B done, he done, can be reported to the A, let A subsequent work to do .

The basic mechanism of usage Handler

Faith pragmatism, or start using the angle Handler Handler's speaking to sum up the usage of it.

Create an instance Handler

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        Log.d(TAG, "handleMessage: msg.what = " + msg.what + " msg.arg = " + msg.arg1);
        switch (msg.what) {
            case OVER:
                text.setText("下载完成");
                break;
            case PROGRESS:
                text.setText("已完成" + msg.arg1 + "%");
                break;
            default:
                break;
        }
    }
};

HandleMessage need override method is to pass is determined by the message type msg.what, to enter a different service processing logic.

Incoming handler to the child thread

class DownloadThread extends Thread {
   Handler handler;
   public DownloadThread(Handler handler) {
       this.handler = handler;
   }
}

When it needed from the child thread return a message

Example simulation of a download operation, namely the sub-thread download incoming handler, the handler with the timing of a configuration Message, back to the main thread updates the display TextView.

@Override
public void run() {
    while (progress < 100) {
        Message response = Message.obtain(handler);
        response.what = PROGRESS;
        progress += 5;
        if (progress == 100) {
            response.what = OVER;
        }
        response.arg1 = progress;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        response.sendToTarget();
    }
}
  • what
    used to mark the message type, such as PROGESS, OVER which are used to represent the current state of the download.
  • arg1
    is used to store the value of other messages, as described in Example arg1 for storing the download progress.

Epilogue

Okay, so a simple demo handler mechanism is complete. Android framework for us to complete setting up the mechanism observer, we need to do is to construct a good handler in the business processing logic in the main thread and the child thread of messages to feedback.

The complete code

发布了20 篇原创文章 · 获赞 13 · 访问量 2万+

Guess you like

Origin blog.csdn.net/kiba_zwei/article/details/86360743