Android: Child threads Send messages to the UI main thread

Source: Internet
Author: User

In Android, the UI thread does not agree to be blocked. So we're going to put the time-consuming work into a sub-thread to deal with.
How do you notify the UI thread when the child threads are time-consuming to process?

We can create a handler object in the main thread of the UI, and then, by overriding its Handlemessage (Message msg) method, it receives SendMessage (message msg) of the handler object in the child thread. Send back the message. This is the end of the work.

And the message about the mainline Cheng thread can be seen in my last blog post. The Looper class and its two important methods and implementation principles are discussed.

In Android, when the UI main thread itself has implemented the Looper functionality. So don't worry about it. So it's easy for a child thread to send a message to the main thread.

Write a sample below. A child thread sends a number to the UI main thread every second. The main thread of the UI will display the number on its own TextView control.

Well, let's take a look at it.

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):

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

Android: Child threads Send messages to the UI main thread

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.