Android Handler introduction and androidhandler Introduction

Source: Internet
Author: User

Android Handler introduction and androidhandler Introduction

Handler usage:

Handler processes UI updates based on received messages. The Thread sends a message to notify Handler to update the UI.

Handler mHandler = new Handler (){
Public void handleMessage (Message msg ){
Switch (msg. what ){
Case 0x1:
View. invalidate ();
Break;
}
Super. handleMessage (msg );
}
};

Class UpdateViewThread implements Runnable {
Public void run (){
While (! Thread. currentThread (). isInterrupted ()){
Message message = new Message ();
Message. what = 0x1;
MHandler. sendMessage (message );
Try {
Thread. sleep (200 );
} Catch (InterruptedException e ){
Thread. currentThread (). interrupt ();
}
}
}
}

(1) to refresh a View in the thread as the interface of the base class, you can use the postInvalidate () method to process it in the thread. In addition, some rewriting methods such as postInvalidate (int left, int top, int right, int bottom) to refresh a rectangular area and perform delayed operations, such as postInvalidateDelayed (long delayMilliseconds) or postInvalidateDelayed (long delayMilliseconds, int left, int top, int right, int bottom) method, in which the first parameter is millisecond

(2) You can call the postMessage or sendMessage method of the Handler object in the run method of a thread. The Android Program maintains a message queue and performs polling.

(3) In Android, every Thread is followed by a logoff. logoff can help the Thread maintain a message queue, but logoff has nothing to do with Handler, from the source code, we can see that Android provides a Thread inheritance class HanderThread to help us deal with it. In the HandlerThread object, we can use the getLooper method to obtain a Looper object control handle, we can map this logoff object to a Handler to implement a thread synchronization mechanism. The execution of the logoff object needs to be initialized and The logoff can be called. prepare, use logoff. release resources using the release method.

(4) for Android, Handler can pass some content, and the Bundle object can encapsulate String, Integer, and Blob binary objects, we use the sendEmptyMessage or sendMessage method of the Handler object in the thread to transmit a Bundle object to the Handler processor. The Handler class provides the override method handleMessage (Message msg) to determine, and identify each piece of information through msg. what. Unpackage Bundle to update the content in the UI thread of the Handler class to refresh the control. Related Handler objects related to sendXXX message sending methods are as follows, and there are postXXX related methods. One is to directly return after sending, and the other is to return after processing.

(5) Android provides a processing method different from the thread, namely Task and AsyncTask. From the source code, we can see that it is a Concurrent encapsulation, developers can easily process these asynchronous tasks.

 

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.