Deep analysis of UI main thread and child thread in Android _android

Source: Internet
Author: User
Tags gettext

In this paper, the UI main thread and child threads in Android are deeply analyzed. Share to everyone for your reference. Specifically as follows:

When an Android program starts running, a process is started separately. By default, all activity or service (service and activity in this program is just two of the components provided by Android, plus content provider and broadcast Receiver) will run in this process.

An Android program has only one process by default, but a process can have many thread. Among so many thread, there is a thread, which we call UI thread. UI thread is created when the Android program runs and is the main thread in a process, primarily responsible for controlling the display, updating, and control interaction of the UI interface. At the beginning of the Android program, a process presented a single-threaded model with all the tasks running in one thread. Therefore, we think that every function performed by UI thread should take as little time as possible. and other more time-consuming work (access to the network, download data, query database, etc.), should be sent to the child thread to execute, so as not to block the main thread.

So how does UI thread work with other thread? The common method is: the birth of a main thread of the handler object, as listener to allow the child thread to push the message into the main thread of the messages Quene, in order to trigger the main thread of the Handlermessage () function, so that the main thread know the state of the child threads, and updates the UI on the main thread.

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

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.

It means that the UI cannot be updated in a child thread. To do this, we need to update the interface by handler the object, notifying the main thread UI thread.

As follows, first create a handler to listen for the events of the message:

Private final int update_ui = 1;
Private Handler Mhandler = new MainHandler ();

Private class MainHandler extends Handler {
@Override public
void Handlemessage (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 (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, a message is issued in the child thread to notify the update UI.

Mhandler.sendemptymessagedelayed (update_ui, 0);

In our program, many callback methods are sometimes not run in the main thread, so if you fail to update the UI in the callback method, you can also use the above method.

I hope this article will help you with your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.