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