Analysis on the use of handler in Android

1. Handler uses export

Now, as a customer, there is a need to start the countdown when opening the activity interface, and jump to a new interface after the countdown (friends with active thinking may immediately think that if the countdown is automatic after opening, it is similar to the welcome screen page of each app), as shown in the following figure:

As a beginner, you may think it's OK to directly start a sub thread containing an inverted loop. The specific implementation is as follows:

1.1 the layout interface code is as follows:

1.2 Java implementation code is as follows:

The logic is very simple, but when you click to enter the interface, you will find that the program has crashed. The error log in logcat is as follows (only UI threads can change the UI interface):

Therefore, we find that in Android development, for example, in the above example, we often complete some operations through a thread, and then synchronously display the corresponding view control UI. Through the above example, we also know that Android cannot directly update the UI through sub threads. In this case, Android provides a set of asynchronous message processing mechanism handler.

2. Implementation method of handler

Using the handler implementation, modify the Java code main2activity.java as follows:

3. Implementation principle of handler

Asynchronous message processing using handler mainly consists of message, handler, messagequeue and looper:

(1) Message, a message passed between threads, used for data interaction between different threads. The what field in the message is used to mark and distinguish multiple messages, the arg1 and arg2 fields are used to pass int type data, and OBJ can pass any type of field.

(2) Handler, used to send and process messages. SendMessage () is used to send messages and handlemessage () is used to process messages and perform corresponding UI operations.

(3) Messagequeue, message queue (first in first out), is used to store messages sent by the handler. A thread has only one message queue.

(4) Looper can be understood as the manager of the message queue. When a message is found in the messagequeue, looper will pass the message to the handlemessage () method. Similarly, a thread has only one looper.

The implementation principle of handler is shown in the figure below:

Combined with the above code example and the implementation process above, to use handler to realize asynchronous message processing, first we need to create a handler object in the main thread and override the handlemessage () method, and then create a message object when UI operations are required in the child thread and send the message through handler R. After that, the message will be added to the queue of messagequeue for processing, while looper will always try to get the message to be processed from messagequeue, and finally distribute it back to the handler's handlemessage () method. Since halldler is created in the main thread, the code in the handlemessage () method will also run in the main thread, so as to realize the purpose of the child thread to realize UI thread operation through the handler mechanism.

4. Handler memory leak analysis

4.1 introduction of handler memory leakage:

In the above handler implementation code, the following questions will be prompted in Android Studio:

The general meaning is that the handler class should be static, otherwise memory leakage will occur. The reason is also clear. If handler is declared as a non static internal class or anonymous class, it may prevent garbage collection of external classes (you can learn about Android's GC collection mechanism). Excessive memory leakage makes the memory occupied by the program exceed the system limit, resulting in oom (memory overflow) and program error.

4.2 prevent memory leakage caused by handler:

Method 1: protect through program logic:

(1) Stop the corresponding background thread when closing the activity. Thread stopping is equivalent to cutting off the line between the handle and the external link, and the activity will naturally be recycled at the right time.

(2) If the handler is referenced by the message of delay, use the removecallbacks () method of the handler to remove the message object from the message queue.

Method 2: declare the handler as a static class. The static class does not hold the object of the external class, so the activity can be recycled at will. The weak reference WeakReference is used here, that is, when the memory is insufficient, the system will destroy / recycle the objects referenced by the weak reference, so as to optimize the memory. The optimized code is as follows:

5. Summary

This time, the effect of using handler to realize the jump of countdown page is realized. We just briefly introduce the use method and precautions of handler, but there are still bugs. If you exit the activity when the countdown is not completed, the sub thread will still run in the background until the jump is completed. The effect and log are as follows:

In view of this situation, it is troublesome for me to deal with it. I need to terminate the thread while destroying the countdown activity. I tried many methods but failed to implement them.

In fact, to achieve the countdown screen flashing effect, you can use a countdowntimer class in Android. A brief implementation will be introduced later.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

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