Android asynchronous task setting timeout using handler update notification function

Considerations for setting request timeout using asynctask for Android

2. Detailed explanation of handler

A handler allows you to send and process messages and runnable objects related to a thread's message queue. Each handler instance is related to a single thread and its message queue. When you create a new handler, it will be bound to the thread / message queue that created it. After that, it will deliver messages and runnable objects to the message queue, and then execute them.

There are two main reasons for using handler:

(1) Scheduling and processing messages and runnable objects at a certain point in the future;

(2) Put the operations to be executed into other threads instead of their own;

Dispatch and process messages by calling post (runnable), postattime (runnable, long), postdelayed (runnable, sendemptymessage (int), SendMessage (message), sendmessageattime (message, long) and sendmessagedelayed (message, long) And other methods. The Post version allows you to put runnable objects into the message queue; the SendMessage version allows you to put a message object containing a bundle object into the message queue and hand it over to the handlemessage (message) method for processing. (this requires you to copy the handlemessage method of the handler)

Handler is very commonly used in actual development. It is mainly used to receive the data sent by the sub thread, and then the main thread updates the interface UI combined with this data.

When the Android application starts, it will start a main thread (i.e. UI thread) to manage the UI controls in the interface and distribute events. For example, click a button, and Android will distribute events to the button to respond to your operation. However, when you need to perform a time-consuming operation, such as IO operation, network communication, etc., if the execution time exceeds 5S, Android will pop up a "classic" anr no response dialog box, and then prompt to press "force quit" or "wait". The way to solve this problem is: we put some time-consuming operations into sub threads to execute. However, because the sub thread involves UI update, and the Android main thread is thread unsafe, the operation of updating UI can only be executed in the main thread. If it is executed in the sub thread, there will be problems. Therefore, a mechanism is needed: the main thread can send a "command / task" to the sub thread for execution, and then the sub thread feeds back the execution result;

1、 What is a handler?

A: handler is a mechanism provided by Android to update the UI and a message processing mechanism. We can send messages or process messages through it.

2、 Then why use handler? Can I not?

A: I searched the Internet and gave me the answer that it must not work. Because Android encapsulates a set of message creation, delivery and processing at the time of design. If the UI information cannot be updated if it is not followed, an exception will be reported.

3、 Why should Android design to update the UI only with the handler mechanism?

A: the most fundamental purpose is to solve the problem of multithreading concurrency!

For example, if there are multiple threads in an activity without locking, the interface will be disordered. However, if these UI update operations are locked, the performance will be degraded.

In consideration of performance, Android provides us with a mechanism to update the UI. We only need to follow this mechanism. There is no need to deal with the problem of multithreading. All UI update operations are trained in turn in the message queue of the main thread.

4、 What are the principles of handler, looper and messagequeue?

A: as we all know, handler has two functions: sending messages and processing messages. The message sent by the handler must be sent to the specified messagequeue, that is, if you want the handler to work normally, there must be a messagequeue, but the messagequeue is related by the looper. Therefore, it can also be said that for the handler to work normally, there must be a looper object in the current thread. (please read carefully)

1. Take a look at the constructor source code provided by looper, as shown in the figure:

You will find that the constructor uses private, which tells the programmer that you cannot create a looper object through the construction method. In the method, looper creates a messagequeue associated with the. This messagequeue is used to manage the message (message object received and processed by the handler)!

2. In order to ensure that the current thread has a looper object, there are two cases to deal with. (crazy Android handout page 224)

(1) When the main UI thread starts, the system initializes a looper object. Just create a handler directly in the program, and then use the handler to send and process messages.

(2) The programmer creates a thread by himself. At this time, he has to manually create a looper object. Create a looper object and call its prepare(). The prepare () method is to ensure that there is at most one looper object per thread.

The source code of the prepare () method is shown in the figure below.

Then start it with loop. Loop (). At this time, the loop () method will continuously fetch the messages in messagequeue () using an endless loop and distribute the messages to the corresponding handler for processing.

To sum up:

-1: The role of looper: each thread has only one looper. It is responsible for managing the messagequeue. It will constantly take messages from the messagequeue and distribute them to the handler of the object - 2: the role of messagequeue: managed by looper, while messagequeue adopts advanced methods to manage messages- 3: The role of handler: it sends messages to the messagequeue managed by looper and is responsible for processing the messages distributed to him by looper.

6、 The five most common ways Android updates UI in sub threads

1. Runonuithread() method

2. Handler. Post() method

3. Handler. Sendmessage() method

4. View. Post() method.

5、view postDelayed(Runnable,long)

summary

The above is the Android asynchronous task setting timeout introduced by Xiaobian. I use the handler update notification function to help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>