Common android handler and message Methods

Source: Internet
Author: User

1.

Private class asyncservicehandler extends handler {

Asyncservicehandler (Android. OS. low.logoff ){
Super (logoff );
}

@ Override
Public void handlemessage (Message MSG ){
...........................................
Case wifimanager. enable_traffic_stats_poll :{
...........................................

}

Private asyncservicehandler masyncservicehandler;

Masyncservicehandler = new asyncservicehandler (wifithread. getlooper ());

Message MSG;

MSG = message. Obtain (masyncservicehandler, wifimanager. enable_traffic_stats_poll, 0, 0 );

MSG. sendtotarget ();

Here message. obtain indicates the obtain message object from the global pool. The first parameter indicates the handler target sent, the second parameter is what, the third parameter is arg1, and the fourth parameter is arg2, you can see the function prototype:

Public static message obtain (handler H, int what, int arg1, int arg2 ){
Message M = obtain ();
M.tar get = h;
M. What = what;
M. arg1 = arg1;
M. arg2 = arg2;

Return m;
}
Finally, call msg. sendtotarget () to send the message to asyncservicehandler. asyncservicehandler inherits and overwrites the handlemessage method, and processes the received message according to the switch in the handlemessage.

2.

Handler mhandler = new handler (){
@ Override
Public void handlemessage (Message MSG ){
Super. handlemessage (MSG );
Int what = MSG. What;

Switch (what ){

Case 2:

String response filenames = msg. getdata (). getstring ("packagename ");

........................ Your own solution ............................

}

};

Message MSG = mhandler. obtainmessage ();
Bundle B = new bundle (); // store data
MSG. What = 2;
B. putstring ("packagename", using filenames. Get (I ));
MSG. setdata (B );
Mhandler. sendmessage (MSG); // sends a message to handler to update the UI

The principle is the same as that of above 1. It also gets the message from the message pool, so that you don't need a new one, which can save resources. New bundle () is used to store data and then mhandler. sendmessage sends messages to handler

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.