Android Development notes — some summary about handler (Part I) — Android Development notes — about the use of asynctask

Following the previous article @ l_404_0 @, today we will talk about another asynchronous operation class handler that is "the top priority" in Android development.

Today, I'm going to talk about some basic definitions and usage of handler

Let's take a downloaded picture as an example. Let's see the example effect first:

OK, let's first look at the definition of handler:

The above is the official description of the hanler class, which roughly means that the handler is mainly used for asynchronous message processing: when a message is sent, it first enters a message queue, and the function sending the message returns immediately, while the other part takes out the messages one by one in the message queue, and then processes the messages, That is, sending and receiving messages are not synchronous processing. This mechanism is usually used to deal with relatively time-consuming operations.

1. Handler is a mechanism to update the UI. It performs time-consuming operations in the child thread, and then notifies the main thread to update the UI.

2. Handler is also a set of message processing mechanism, which can be used to send and process messages.

About the purpose of handler:

The handler can distribute message objects and runnable objects to the main thread, and each handler instance will be bound to the thread that created it (usually in the main thread)

Purpose: 1. Arrange messages or runnable to be executed somewhere in a main thread. 2. Arrange an action to be executed in different threads

Next, let's talk about the basic use of handler. Today, we won't talk about too complex and introduce the concepts of thread and message queue. Let's talk about it in the next article.

Let's start with a textbook version of handler usage: (full comments)

The message here is the message mentioned above. There is a special need here. The main thing is how to get an instance object of a message. The official does not advocate that we directly use new message (). It provides many methods for us to get it. For details, see the API file:

Handler class:

Message class:

Why not go directly to the new message () object? Friends who have checked the source code can find that, in fact, message Obtain() or handler Obtainmesssage() in the source code, we can find that Android provides us with a message pool with a size of 50 and locked. When we call these methods, the system will first go to the message pool to get the message object. If it does not exist, it will go to new to get a new message object.

Message source code:

Here is another example of the above API:

It's very simple. Just refer to the specifications given by the API.

Message object, which not only provides us with the storage object obj, but also provides us with some other types of storage variables, such as:

These variables are light consumption variables provided by Android. We can use them. For example, arg1 and arg2 can be used to store simple integer variables, what can be used to store message identifiers, and then use a switch in handmessgae (message MSG) to determine which operations to perform. The usage is consistent with the code given above.

For details, please check the API. In combination with the comments I just gave, they are actually very simple. Here are no examples one by one.

Finally, let's look at the following APIs for sending messages:

Say a few confusing ones. Others can try to play by themselves

For example, sendmessagedelayed is the time to delay sending back a message, followed by a long type of time, in milliseconds.

In addition, sendmessattime is to send messages regularly, and the time is delivered by uptimemills()

These two sentences are equivalent. They both add messages to the queue with a delay of 1 second:

Well, let's introduce so much first. Others will be discussed in the next article.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>