This article analyzes the main thread and child thread of UI in Android in more depth. Share it for your reference. The details are as follows:
When an Android program starts running, a Process will be started separately. By default, all Activity or Service in this program (Service and Activity are only two of Components provided by Android, in addition to Content Provider and Broadcast Receiver) will run in this Process.
An Android program has only one Process by default, but there can be many Threads under a Process. Among so many Threads, there is a Thread, which we call UI Thread. UI Thread is created when the Android program is running. It is a main thread Main Thread in Process, which is mainly responsible for controlling the display, update and control interaction of the UI interface. At the beginning of Android program creation, a Process presented a single-threaded model, and all tasks were run in one thread. Therefore, we believe that the shorter the time it takes for each function executed by UI Thread. Other time-consuming tasks (accessing the network, downloading data, querying databases, etc.) should be left to the child thread to perform to avoid blocking the main thread.
So, how does UI Thread work with other Threads? The common method is: give birth to a Handler object of the main thread, and act as a Listener to allow the child thread to push the message into the Message Quene of the main thread, so as to trigger the handlerMessage() function of the main thread, let the main thread know the state of the child thread, and update the UI in the main thread.
For example, when the state of the child thread changes, we need to update the UI. If you update the UI directly in the child thread, the following exception will usually be thrown:
11-07 13:33:04.393: ERROR/JavaBinder(1029):$CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its views.
Meaning, the UI cannot be updated in the child thread. To do this, we need to notify the main thread Ui Thread through the Handler object to update the interface.
As follows, first create a Handler to listen for Message events:
private final int UPDATE_UI = 1; private Handler mHandler = new MainHandler(); private class MainHandler extends Handler { @Override public void handleMessage(Message msg) { switch () { case UPDATE_UI: { ("TTSDeamon", "UPDATE_UI"); (().toString()); ShowAnimation(); break; } default: break; } } }
or:
private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch () { case UPDATE_UI: { ("TTSDeamon", "UPDATE_UI"); (().toString()); ShowAnimation(); break; } default: break; } } }
When the state of the child thread changes, a message is issued in the child thread to notify the update of the UI.
(UPDATE_UI, 0);
In our program, many Callback methods sometimes do not run in the main thread, so if the UI is updated in the Callback method fails, the above method can also be used.
I hope this article will be helpful to everyone's Android programming design.