The method used for thread threading in Android is the same as the Java SE. Like most OS systems, Android also has a main thread called UI thread. UI Thread is primarily used to distribute messages to the appropriate widget, including drawing (Drawing) events. UI thread is also the thread used to handle user interaction events. For example, if you press a button on the screen, the UI thread notifies the corresponding control (Widgets) of the touch event, and the Widget sets its status to "down" and sends the "redraw" (Invalidate) incident to the event queue. The UI thread notifies widgets to redraw itself after reading events from the event queue.
If your application is poorly designed, this single-threaded mode of the UI thread can result in very poor user response performance. Especially if you put some time-consuming operations such as network access or database access on the UI thread, which can cause the user interface to be unresponsive, and worst of all, if the UI thread blocks for more than a few seconds (5 seconds), the famous ANR dialog box appears:
So when designing an application, you need to run some time-consuming tasks using a separate work thread to avoid blocking the UI thread, but if you want to update the UI thread in the worker thread, you cannot update the UI directly in the worker thread because the UI thread is not "thread Safe." Therefore, all UI-related operations typically have to be done in UI thread.
The Android OS provides several ways to access the UI thread with a non-UI thread.
Activity.runonuithread (Runnable)
View.post (Runnable)
View.postdelayed (Runnable, long)
Handler