Android: Child threads Send messages to the UI main thread

Source: Internet
Author: User

In Android, the UI thread is not allowed to be blocked, so we have to put the time-consuming work into the sub-thread to handle.
So how do you tell the UI thread when a child thread takes time to process?

We can create a handler object in the main thread of the UI, and then, by overriding its Handlemessage (Message msg) method, the method receives SendMessage (message msg) of the handler object in the child thread. Send back the message. This will complete the work by a single pick-up;

And about the main thread Cheng the content of the message can be seen in my previous blog, which describes the Looper class and its two important methods and implementation principles.

In Android when the UI main thread itself has implemented the function of looper, so we do not worry about, so the child thread to the main thread to send messages is very simple;

As an example, the child thread sends a number to the UI main thread every second, and the UI main thread displays the number on its own TextView control.

Okay, so let's take a look at the run:

The following code is attached:

Mainactivity.java:

 PackageActivity.wyc.com.threaddemo;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.os.SystemClock;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.TextView; Public  class mainactivity extends actionbaractivity {    PrivateTextView Tvobj;PrivateHandler Handler;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Tvobj = (TextView) Findviewbyid (R.id.tvid); Handler =NewHandler () {@Override             Public void Handlemessage(Message msg) {Super. Handlemessage (msg);            Tvobj.settext (string.valueof (MSG.ARG1)); }        };NewThread (NewRunnable () {@Override             Public void Run() {intnum =0; while(true) {Message message = Message.obtain ();                    MESSAGE.ARG1 = num;                    Handler.sendmessage (message); Systemclock.sleep ( +);                num = ++num;    }}). Start (); }}

Activity_main.xml:

<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"android:paddingleft="@dimen/activity_horizontal_margin"android:paddingright="@dimen/activity_horizontal_margin"android:paddingtop="@dimen/activity_vertical_margin"android:gravity="Center_horizontal"Android:paddingbottom="@dimen/activity_vertical_margin"tools:context=". Mainactivity "> <textview android:id="@+id/tvid"Android:textsize="40SP"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"/></linearlayout>

The following is the source code (Baidu cloud disk, Android studio written):

Http://pan.baidu.com/s/1kT9oKbP

Android: Child threads Send messages to the UI main thread

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.