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