Http://alex-yang-xiansoftware-com.iteye.com/blog/850865
Handler is mainly used for communication between threads. If the activity or service needs to accept messages from other threads, the handler. Callback interface must be implemented in the activity or service that needs to receive messages. This interface is implemented:
Java code
- Public Boolean handlemessage (Message inmessage );
Method. Then, the callback class instance is passed in at the place where handler is created:
Java code
- Logoff logoff = incontext. getmainlogoff ();
- Mthread = logoff. getthread ();
- Mhandler = new handler (logoff, mcallback );
Or:
Java code
- Mhandler = new handler (mcallback)
Then, you can use the following code to send messages to acitity or service:
Java code
- // The token is an object that can be passed in with other parameters.
- Mhandler. obtainmessage (inwhat, INA, INB, Token Ken)
- Mhandler. sendmessage (inmessage)
Or
Java code
- Mhandler. sendemptymessage (inwhat)
After the preceding code is executed, the handlemessage of the callback instance will be called when the handler is created.
You can use the following code to put a thread instance into handler for execution:
Java code
- Mhandler. Post (inrunnable );
You can use the following code to delete this thread:
Java code
- Mhandler. removecallbacks (inrunnable );
Handler also has many similar methods to send messages or add threads. Added some other features