How Android Handle works

Source: Internet
Author: User

How Android Handle works


First of all, we need to understand the usage of Handle, which is the use of Handle. If Handle is used to do something and understands this, I will understand the following content.

I: Next I will explain the purpose of Handle.

What is Handle? Why should we use Handle? Handle is actually a little similar to Intent. Here I am going to say something similar, but I didn't say anything like this, intent is used to implement communication between the four major components. What about Handle? This is used to implement communication between threads. For example, when a sub-thread performs a time-consuming operation, we do not know when it is finished or when it is finished, do we need something to inform us and let us know? So there is a Handle source, and there is no Handle, which is very troublesome for us.


Ii. Handle's message mechanism Principle

1. Graphic Display

Download the image and notify the main thread to update the ui after the download is complete)

1. There is a message processor Handle in the main thread, which is placed in the message queue in the main thread.

2. There is a MessageQueue in the main thread, that is, the message queue, which stores messages.

3. There is also a loose in the main thread (non-stop loop message queue to retrieve messages). If there is a new message, the message is sent to the message processor. handle will call the handleMessage in the thread to process the message.


3. Implementation

1. The sub-thread uses handle to send messages and place them in the message queue of the main thread.

2. There is a loose loop in the main thread to get messages

3. When a new message is found, the handlemessage method is called to process the message.


Iii. Sample Code:

Package com. zengtao. classwork;


Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. support. v7.app. ActionBarActivity;
Import android. widget. ProgressBar;


Public class MainActivity extends ActionBarActivity {
Private ProgressBar pb; // progress bar
Private int [] randData = new int [100]; // Array
Private int index = 0; // index
Private int mProgressStaus = 0; // you can specify the length of the progress bar.
Private Handler mHandler;


@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );


Pb = (ProgressBar) findViewById (R. id. pb );


// Initialize handle and bind it to the queue message in the main thread
MHandler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
// Receives the message
If (msg. what = 0x111 ){
MProgressStaus = index;
Pb. setProgress (mProgressStaus );
}
}
};
// Create a subthread to process time-consuming work in the subthread
New Thread (){
@ Override
Public void run (){
Super. run ();
While (index <100 ){
DoWork ();
Message msg = new Message ();
Msg. Why = 0x111;
MHandler. sendMessage (msg );
}
}


}. Start ();
}


/**
* Simulate time-consuming work (must be done in the Child thread)
*
* @ Return indicates the progress bar.
*/
Public int doWork (){
RandData [index ++] = (int) (Math. random () * 100 );
Try {
Thread. sleep (100 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
Return index;
}
}


Iv. sample code annotation

1. Create a Handle object in the main thread

2. send messages to the queue in the Child thread

3. The main thread logoff gets the message and uses the handleMessage () method for processing.



The above describes how to use handle and how it works. If you do not understand it, please forgive me and share it with you. I hope it will help you.


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.