About the use of Looper

Source: Internet
Author: User
Tags message queue prepare stub
We know that in the Android thread, the message loop is taken out of the information to deal with, the main thread and child threads, child threads and child threads between the communication is also looper to achieve, and then I will briefly introduce the use of the Looper method. The main thread (that is, the UI thread) itself has a message loop that does not need to be created, and other threads need to be created manually, using prepare () to create the loop, and loop () to start the loop until the loop stops.
Method Introduction: Getmainlooper () returns the Looper of the application main thread
GetThread () returns the current thread
Loop () running information queues in threads (Message queue)
Mylooper () returns the looper of the current thread
Myqueue () returns the MessageQueue object for the current thread
Prepare () initializes the looper of the current thread
Preparemainlooper () initializes the looper of the application main thread
Quit () Exit Looper
1. Mainline Cheng line Cheng information: First create a child thread, create a looper in the child thread and run, receive the message sent by the mainline Cheng thread, and process it. Childrenlooperthread childerlooperthread=new Childrenlooperthread ();
Childerlooperthread.start ();
public class Childrenlooperthread extends thread{
@Override
public void Run () {
Looper.prepare ()//Create Looper
Mhandler=new Handler () {
@Override
public void Handlemessage (msg) {
TODO auto-generated Method Stub
LOG.E ("TAG", "Mhandler");
LOG.E ("TAG", msg.obj.toString ());//receive the message sent by the mainline Cheng thread and process.
Super.handlemessage (msg);
}
};
Looper.loop ()//Run
Super.run ();
}
}
Sends information message msg=mhandler.obtainmessage () to a child thread in the main thread;
msg.obj= "Main Line Cheng thread pass data";
Mhandler.sendmessage (msg);
2. The child thread sends the message to the main thread first creates the child thread, through the main thread handler sends the information, handles the main thread to send the information in the handler handlermessage which the main thread creates. Mainlooperthread mainlooperthread=new Mainlooperthread ();
Mainlooperthread.start ();
public class Mainlooperthread extends thread{
@Override
public void Run () {
Message Msg=mainhandler.obtainmessage ();
msg.obj= "Child threads pass data to the main thread";
Mainhandler.sendmessage (msg);
Super.run ();
}
}
Mainhandler=new Handler (Looper.getmainlooper ()) {//is created through loope of the main thread Handler
@Override
public void Handlemessage (msg) {
TODO auto-generated Method Stub
LOG.E ("TAG", "MainHandler");
LOG.E ("TAG", "handlemessage");
Toast.maketext (Mainactivity.this, msg.obj.toString (), Toast.length_short). Show ();
Super.handlemessage (msg);
}
};
This allows you to communicate between any 2 threads.

There are points link: http://download.csdn.net/detail/jhg19900321/4561747

No points link: https://code.google.com/p/my-csdn-resource/

Note: I think that sharing should be free, the reason there is a link to points, is to have a lot of friends, that is to choose to have points link, give me some points, so I can download other people's source code, if friends do not have points, then you can choose the link without points, thank you.


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.