The use principle of Android handle understanding

Source: Internet
Author: User


The use of handle, first of all we have to understand that that is the usefulness of handle, handle is used to do what, understand this point, then I come to understand the following content.

A: Next I'll explain the use of handle.

Handle in the end is what, why we want to use handle,handle in fact, and intent a bit similar, here I say something like, no said what is the same ah, such words, not I said, intent is used to achieve the four components of communication between, and Handle it? The purpose of this is to implement the communication between the threads, for example: when the sub-thread does a time-consuming operation, we do not know when he is finished, do not know, then, we need a thing to inform us, let us know, so said, there is a source of handle, Without handle this thing, it's very troublesome for us.


Two: The principle of the message mechanism of handle

1. Graphic display

2. Internal parsing: Purpose (download picture in sub-thread, notify main thread update UI after download is completed)

1. The main thread has a message processor handle, 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 that holds the message

3. In the main thread, there is also a looper (Nonstop loop Message Queue fetching messages) if a new message is sent to the message processor, handle calls the handlemessage in it to process the message


3. Specific implementation

1. Sub-threads, using handle to send messages in the main thread's message queue

2. There is a looper loop inside the main thread to get the message

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


Three: 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; Set 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);


Initializes the handle, which is bound in the queue message in the main thread
Mhandler = new Handler () {
@Override
public void Handlemessage (Message msg) {
Super.handlemessage (msg);
Receiving messages
if (msg.what = = 0x111) {
Mprogressstaus = index;
Pb.setprogress (Mprogressstaus);
}
}
};
Create child threads to process time-consuming work in child threads
New Thread () {
@Override
public void Run () {
Super.run ();
while (Index < 100) {
DoWork ();
Message msg = new Message ();
Msg.what = 0x111;
Mhandler.sendmessage (msg);
}
}


}.start ();
}


/**
* Simulate time-consuming work (be sure to do it in a sub-thread)
*
* How much @return the progress bar
*/
public int doWork () {
randdata[index++] = (int) (Math.random () * 100);
try {
Thread.Sleep (100);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
return index;
}
}


Four: Sample code annotations

1. Create a handle object in the main thread

2. Sending messages to the queue in a child thread

3. The main thread looper gets the message and uses the Handlemessage () method to process



The above is the use of handle, as well as the principle of the analysis, write do not understand, please understand, share this to everyone, I hope you have some help.


How the handle of Android is understood

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.