Android -- Handler usage: update the interface in the Child thread

Source: Internet
Author: User

Android -- Handler usage: update the interface in the Child thread

This article describes how to use Android Handler. Handler can send Messsage and Runnable objects to the Message Queue of the associated thread. Each Handler object is associated with the thread that created it, and each Handler object can only be associated with one thread.

  1. Handler generally has two purposes: 1) to execute a scheduled task, you can schedule it to execute some tasks and simulate the timer. 2) Inter-thread communication. When an Android Application is started, a main thread is created, and the main thread creates a message queue to process various messages. When creating a sub-thread, you can get the Handler object created in the parent thread in your sub-thread to send messages to the Message Queue of the parent thread. Because Android requires updating the interface in the UI thread, you can update the interface in other threads through this method.

    ◆ Use Runnable to update the interface in the Child thread

    Create Handlerpublic class HandlerTestApp extends Activity {Handler mHandler; TextView mText;/** Called when the activity is first created in onCreate. * // @ 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 a Runnable object and update the interface in runnable. Here, we modify the text of TextView. It must be noted that the Runnable object can be created in the main thread or child thread. We created it here in the Child thread.
    Runnable mRunnable0 = new Runnable()     {                 @Override                 public void run() {                         // TODO Auto-generated method stub                         mText.setText("This is Update from ohter thread, Mouse DOWN");                 }     };

    Create a sub-thread. In the run function of the thread, we send a runnable to the Message Queue of the main thread to update the interface.
    Private void updateUIByRunnable () {new Thread () {// Message msg = mHandler. obtainMessage (); public void run () {// mText. setText ("This is Update from ohter thread, Mouse DOWN"); // This statement will throw an mHandler exception. post (mRunnable0 );}}. start ();}

     

    ◆ Use Message to update the interface in the Child thread

    The Message update interface is similar to the Runnable update interface, but it only needs to be modified.
    Implement your own Handler and process the Message. private class MyHandler extends Handler {@ Override public void handleMessage (Message msg) {// TODO Auto-generated method stub super. handleMessage (msg); switch (msg. what) {case UPDATE: // when receiving a message, UPDATE mText on the page. setText ("This update by message"); break ;}} send the message private void updateByMessage () in the new Thread {// anonymous object new Thread () {public void run () {// mText. setText ("This is Update from ohter thread, Mouse DOWN"); // UPDATE is a defined integer that represents the Message ID Message msg = 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.