android UI main thread and sub thread in depth analysis

  • 2020-06-15 10:09:55
  • OfStack

In this paper, UI main thread and sub-thread in android are analyzed in depth. Share to everybody for everybody reference. The details are as follows:

When an Android program starts running, a separate Process is started. By default, all Activity or Service in this program (Service and Activity are just two of the Components provided by Android, in addition to Content Provider and Broadcast Receiver) will run on this Process.

A single Android program has only one Process by default, but a single Process can have many Thread. Of these, there is one Thread, which we call UI Thread. UI Thread is created when the Android program is running. It is the main thread of Main Thread, which is responsible for controlling the display, update and control interaction of THE UI interface. When the Android program was created, one Process presented a single-threaded model, with all tasks running in one thread. Therefore, we believe that each function performed by UI Thread should take as little time as possible. Other time-consuming tasks (accessing the network, downloading data, querying databases, etc.) should be done by child threads to avoid blocking the main thread.

So, how does UI Thread work with other Thread1? The common method is to create an Handler object of the main thread, as Listener to let the child thread can send the message Push to the main thread Message Quene, in order to trigger the main thread handlerMessage () function, let the main thread know the state of the child thread, and update UI in the main thread.

For example, when the state of the child thread changes, we need to update UI. If you update UI directly in a child thread, you usually throw the following exception:

11-07 13:33:04.393: ERROR/JavaBinder(1029):android.view.ViewRoot$CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its views.

UI cannot be updated in a child thread. To do this, we need to update the interface by notifying the main thread Ui Thread via the Handler object.

As follows, first create an 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 (msg.what) {
case UPDATE_UI: {
Log.i("TTSDeamon", "UPDATE_UI");
showTextView.setText(editText.getText().toString());
ShowAnimation();
break;
}
default:
break;
}
}
}

Or:


private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_UI: {
Log.i("TTSDeamon", "UPDATE_UI");
showTextView.setText(editText.getText().toString());
ShowAnimation();
break;
}
default:
break;
}
}
}

When the state of the child thread changes, Message is issued in the child thread to notify the update of UI.


mHandler.sendEmptyMessageDelayed(UPDATE_UI, 0);

In our program, many of the Callback methods are sometimes not running in the main thread, so if updating UI in the Callback method fails, you can use the above method.

I hope this article is helpful for your Android programming.


Related articles: