Why is the main thread in Android not due to the infinite loop ANR in Looper.loop?

After learning the Android Handler mechanism, a question arises: Why did Looper.loop () run an infinite loop in the main thread, but did not cause ANR?

First of all: under what circumstances will ANR abnormalities occur?

1. The event has not been processed
2. The event is being processed but not completed in time

For example:
Activity response exceeds 5 seconds
BroadcastReceiver response exceeds 10 seconds
Service response exceeds 20 seconds

Explanation:
Because the entire life cycle of the application is that
Android running in this message loop is driven by events, the Looper.loop method continuously accepts and handles events. Every click, touch or Activity call of each life cycle is in Looper. Under the control of the .loop method, once the Looper.loop method ends, the life cycle of the application ends.
The main method of ActivityThread is to generate a message loop to support the application to continue running after opening. Once the message loop is exited, the application exits. When there is no event in the queue to be processed, the Looper.loop method of the main thread may cause the main thread to block, but as long as the message loop is not blocked, the event can be processed all the time and no ANR exception is generated.

Published 60 original articles · 25 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_41466437/article/details/104628424