Learn some relevant knowledge of the android-handler mechanism

  Handler has been used since we started to learn Android and later in our work. For me, I actually know that it is used to send messages and update the interface. It seems that other things are not very useful. Knowledge is knowing how to use it, and the most fundamental reason is still almost lost. So I sort out what I understand today, and put the referenced literature articles at the end

What is Handler?

Many times we say: " A set of mechanisms for thread-to-thread communication ." It is also said: " handler is a mechanism for updating the UI interface and a mechanism for message processing. We can send messages and process messages "

 

Why is there this Handler mechanism?

Answer: When Android is designed, it encapsulates a set of message creation, delivery, and processing mechanisms. If you do not follow this mechanism, you will not be able to update UI information, and an exception will be thrown.

Why should Android set the UI to be updated only through the Handler mechanism?

Answer: The most fundamental problem is to solve the problem of multi-threaded concurrency;

      Assuming that if in an Activity, there are multiple threads to update the UI, and there is no locking mechanism, what kind of problems will arise? - The update interface is confusing;

      What kind of problems will arise if all operations to update the UI are locked? - performance degradation

      For the consideration of the above problems, Android provides a mechanism for updating the UI, we only need to follow this mechanism.

      Don't worry about multi-threading issues, the operation of updating the UI is polled in the message queue of the main thread.

 

 

What is the mechanism of the handler?

1. The Handler mechanism is composed of Looper and MessageQueue to build a message mechanism .

2. The Handler is responsible for sending the message (initiator), and the Looper is responsible for receiving the message sent by the handler (only acceptable) and sending the message back to the handler itself (and then throwing the received ball back to the handler). A MessageQueue is a container for storing messages (a person of records). Message The specific message sent (what exactly was sent, know here).

What is the function of HandlerThread?

Answer: HandlerThread thread= new  HandlerThread("handler thread"); automatically includes a waiting mechanism , and waits for the Looper to be created before creating the Handler to avoid null pointer exceptions.

 

 Why doesn't the Looper infinite loop cause the application to freeze?

The thread does not have a Looper by default. If you need to use a Handler, you must create a Looper for the thread. The main thread we often mention is also called the UI thread. It is the ActivityThread. When the ActivityThread is created, the Looper will be initialized. This is why the Handler can be used by default in the main thread.

Several ways to update UI in android

There are about four common ones: runOnUiThread, handler post, handler sendMessage, view post

 

Handler handler = new Handler() {

// Implement the handleMessage method in Handler

public void handleMessage(android.os.Message msg) {

// The second method is to get the data from the outside and return it inside

textView.setText("The second method handlerEmptyMessags");

}};

/**

* The first method Handler post

*/

public void handler1() {

handler.post(new Runnable() {

@Override

public void run() {

// Get a text message here

textView.setText("The first method Handler post");

}});}

 

/**

* The second method sendEmptyMessage

*/

public void handler2() {

handler.sendEmptyMessage(1);

}

/**

* The third method runOnUiThread

*/

public void handler3() {

runOnUiThread(new Runnable() {

@Override

public void run() {

textView.setText("The third party method runOnUiThread");

}});}

/**

* The fourth method view

* post----It will judge whether it is currently a UI thread. By default, an action will be sent through Handler.post. If it is the UI thread, execute the run() method

*/

public void handler4() {

textView.post(new Runnable() {

@Override

public void run() {

textView.setText("The fourth method view post");

}});}

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_22576071/article/details/81304282