Parameter passing of the handler class, asynchronous thread, and message class

Source: Internet
Author: User

  Declares a Handlerthread object that implements the function of using looper to handle Message Queuing

This class is provided by the Android operating system
Handlerthread handlerthread = new Handlerthread ("Handler_thread");
Handlerthread.start ();
The current MyHandler object uses the Looper thread to process messages
MyHandler MyHandler = new MyHandler (Handlerthread.getlooper ());
Message msg = Myhandler.obtainmessage ();
Simple Data transfer
Msg.obj = "Zhanglei";
Large data transfer
Bundle B = new bundle ();
B.putint ("Age", 23);
B.putstring ("name", "Zhanglei");
Msg.setdata (b);
Sends MSG to the target object, the so-called target object is the handler object that generated the MSG object
Msg.sendtotarget ();

Declares that the MyHandler class inherits from the handler class:

Class MyHandler extends handler{
Public MyHandler () {

}
Public MyHandler (Looper Looper) {
Super (Looper);
}
Handlemessage method is triggered whenever the MyHandler object executes the SendMessage method
@Override
public void Handlemessage (Message msg) {
TODO auto-generated Method Stub
Super.handlemessage (msg);
String str = (string) msg.obj; Remove the value from obj in msg
Bundle B = Msg.getdata ();
int age = B.getint ("Age");
String name = b.getstring ("name");
}
}

Parameter passing of the handler class, asynchronous thread, and message class

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.