Update interface _android in child threads of Android development tutorial

Source: Internet
Author: User
Tags message queue

Each handler object is associated with the line threads relative that created it, and each handler object can only be associated with one line threads relative.
Handler is generally used for two purposes: 1 execution of Scheduled tasks, you can be scheduled to perform certain tasks, you can simulate the timer. 2) communication between threads. When the Android application starts, a main thread is created, and the main thread creates a message queue to handle the various messages. When you create a child thread, you can then get the handler object created in the parent thread from your child thread, and you can send a message to the parent thread's message queue through the object. Because Android requires updating the interface in the UI thread, you can update the interface in other threads by this method.

An example of updating the interface through runnable in a child thread

Creating Handler in OnCreate

Copy Code code as follows:

public class Handlertestapp extends activity {
Handler Mhandler;
TextView Mtext;
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Mhandler = new Handler ();//Create Handler
Mtext = (TextView) Findviewbyid (r.id.text0);//A TextView
}

Build the Runnable object and update the interface in Runnable, where we have modified the TextView text. What you need to note here is that the Runnable object can be created again in the main thread or in a child thread. We created this here in a child thread.
Copy Code code as follows:

Runnablemrunnable0=newrunnable ()
{
@Override
Publicvoidrun () {
Todoauto-generatedmethodstub
Mtext.settext ("Thisisupdatefromohterthread,mousedown");
}
};

To create a child thread, in the run function of the thread, we send a runnable to the main thread's message queue to update the interface.

Copy Code code as follows:

Privatevoidupdateuibyrunnable () {
Newthread ()
{
Messagemsg=mhandler.obtainmessage ();
Publicvoidrun ()
{
Mtext.settext ("Thisisupdatefromohterthread,mousedown");/This sentence throws an exception
Mhandler.post (MRUNNABLE0);
}
}.start ();
}

Update the interface with message in child threads
Updating the interface with the message is similar to the Runnable update interface, but it needs to be modified in several places.
Implement your own handler and process the message

Copy Code code as follows:

Privateclassmyhandlerextendshandler
{
@Override
Publicvoidhandlemessage (messagemsg) {
Todoauto-generatedmethodstub
Super.handlemessage (msg);
Switch (msg.what)
{
caseupdate://updates the interface when a message is received
Mtext.settext ("Thisupdatebymessage");
Break
}
}
}

Send a message in a new thread

Copy Code code as follows:

Privatevoidupdatebymessage ()
{
Anonymous objects
Newthread ()
{
Publicvoidrun ()
{
Mtext.settext ("Thisisupdatefromohterthread,mousedown");
Update is a defined integer that represents the message ID
Messagemsg=mhandler.obtainmessage (UPDATE);
Mhandler.sendmessage (msg);
}
}.start ();
}

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.