Android development tutorial handle to achieve multithreading and asynchronous processing

Let's talk about handler this time. Why does handler appear? First of all, before the basic controls are basically invoked and processed in the onCreate (Bundle savedInstanceState) method of Activity, in some cases, such as downloading software on the network, some operations that need to wait for a longer response time, if placed in the same way of Activity, then, when implementing this method, The whole activity is immovable and users can only wait. This user experience is very poor. The best result of this processing method is to wait for a period of time and get the desired result. The bad situation is to wait for n long time and there is no result. Some even cause the activity to report an error. In order to avoid these situations, Therefore, the feature of handler is introduced. It is like a thread queue. It is also an asynchronous message processing. First, let's take a look at an example to understand the handler. In the layout file, there are two buttons, start and stop, which control the start and stop of threads respectively.

The code in activity is as follows:

We can see that in the activity, event listeners are bound to the two buttons respectively, an instance of handler is created, and an anonymous inner class is created, which is a thread handler thread that implements the runnable interface. When the start button is pressed, handler.post (handlerthread) will be executed; As mentioned earlier, the handler uses a thread queue. This code adds the handler thread to the handler's thread queue. Because the added handler thread is the first thread, it will immediately execute its run () method. In the run () method, handler. Postdelayed (handlerthread, 3000); Once again, put the handlerthread into the handler's thread queue, where a delay of 3000ms is set. In this way, the whole program will run continuously, and "handlerthread is running..." will be printed in logcat every 3000ms. However, it is worth noting that the emergence of the handler does not separate the thread where these printing operations are located from the main thread. In fact, there are no two threads running here. These printed contents are also run by the main thread. We can do an experiment and print the name of the current thread through thread. Currentthread. Getname() after oncreate function and at the place where the statement is printed. We can see that they are all the same and are all main, which means that they are all run by the main thread. We know that the start () method is required to start a thread. In this program, instead of starting handlerthread, we directly call the run () method. So it's not surprising that the main thread is running. From the above example, if this handler is used in this way, it is not the effect we want, because it does not implement asynchrony and still runs in a main thread. Therefore, we must use handler in another way. To implement asynchronous multithreading of handler, you need to understand the other two classes, one is message class and the other is looper class. There is a message queue in each handler object. The message object stored in the queue can be obtained by using obtainmessage(). At the same time, the message object is used to transfer and use. It can transfer two integers and an object. Try to use the arg1 and arg2 integers of message to transfer parameters, so as to minimize the system consumption (as the API says). If the amount of data transferred is large, you can use the SetData (bundle a) method. The bundle object can be roughly regarded as a map object, However, its keys are all string, while value is a limited number of types, which can be viewed in the API. The looper class has the function of getting messages from the message queue circularly. We can use looper in a thread, so that the thread can get messages in the message queue circularly until the message queue is empty. However, we generally do not directly create and use looper. The looper function is implemented in the handlerthread class provided by Android, so we only need to use the handlerthread class. We use the handlerthread object to call getlooper () to get the looper object of the thread. Let's look at the following example

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