Handler definition: ,mp4 player
major sub-thread to send the data received and used this data with the main thread update the UI.
Explanation: When the application starts, Android will first open a main thread (also is the UI thread), the main thread for the management interface of the UI controls, the event distribution, for example, if you click on a Button, Android will be distributed to the Button on the event, to respond to your operation. If you need a time-consuming operation at this time, for example: read data network, or read the local time when the larger a file, you can not put these operations on the main thread, that if you put the main thread, then screen will appear the phenomenon of suspended animation, if 5 seconds is not yet complete, then, you will receive an error message Android system because the child thread related to UI update,, Android main thread is thread safe, that is, the main thread update the UI to update only the child thread operation is dangerous. This time, Handler has emerged., to to solve this complex problem, because Handler is running in the main thread (UI thread), it sub-thread to pass through the Message object data, this time, Handler to bear the child thread to accept pass over the (sub-thread with sedMessage ( ) method of transmission brother) Message object (which contains data), the main thread of these messages into the queue, with the main thread to update UI.
the general process of using the Handler:
1, first create a Handler object, you can directly use the Handler Handler no argument constructor to create objects, you can also inherit the Handler class, override the method to create handleMessage Handler objects.
2, the listener, call the Handler of the post method to add the object to execute the thread to the thread queue them. At this point the object will be added to the thread to the handler object’s thread queue.
3, the action will be written in the thread object’s run method, generally a Runnable object, the replication methods which run on it.
Handler consists of two queues,Clock Spy Camera HD Clock DVR Motion Detection Camera DVR920 – Family Security Clock, one of which is the thread queue, the other is the message queue. Will use the post method of the Thread object into the thread queue handler to use sendMessage (Message message) the message into the message queue.
If you want this process has been implemented, the internal implementation method you can run postDelayed or post method,men watches, then the thread object to the message queue, repeat. You want the thread to stop execution, calling Handler object removeCallbacks (Runnable r) method to remove the thread from the thread object queue, so that thread to stop execution.
Handler provides a way for the Android asynchronous message handling mechanism, when the message queue to send messages (sendMessage) returns immediately after, and read messages from the message queue will block them from the message queue Handler execution when reading a message in the public void handleMessage (Message msg) method, and therefore should be used when creating Handler anonymous inner classes override this method, the method reads the message written on the operation, use Handler’s obtainMessage () to get the message object.
Handler relationship with the thread:
method using the Handler’s post Handler thread Runnable object into the queue after the implementation of Runnable in fact did not open a separate thread, but still Activity thread of the current,pmp, Handler is called the run method of Runnable objects.
Bundle what:
Bundle is a special map, which is a tool for transmitting information,kid watch, it can only be a string type key and value can only be basic data types common . Handler execution
how to open a new thread when Runnable:
1, the first object to generate a HandlerThread realized Looper to process the message queue using the function, this class provided by the Android Application Framework
HandlerThread handlerThread = new HandlerThread (br> handlerThread.start ();
3, according to the object by one of the HandlerThread Looper object. 4, create a custom inherited from Handler subclass, which objects to achieve a parameter for the Looper’s constructor, method calls the contents of the parent class constructor can be.
5, Looper obtained using the third step the object to create a custom Handler subclass object, then the message (Message) is sent to the message queue of the Handler, Handler replication of handleMessage () will run to process the message in the message queue.
news that Message object, you can pass some information, you can use arg1.arg2, Object or objects to pass a number of plastic surgery, you can also use the Message object setData (Bundle bundle) in terms of Bundle object passed to the newly created thread, the thread in the implementation of the newly created handleMessage (Message msg) from the message when using getData () extracts the Bundle object for processing.
Handler some of the features
handler objects can be distributed Message Runnable object to the main thread, each Handler instance, he will be bound to create a thread (usually located in the main thread),
it has two roles: (1): Runnable arrangements in a message or a place in the main thread execution, (2) to arrange an action in a different thread
Handler Message carve some of the ways
post (Runnable)
postAtTime (Runnable, long)
postDelayed (Runnable long)
sendEmptyMessage (int)
sendMessage (Message)
sendMessageAtTime (Message, long)
sendMessageDelayed (Message, long)
class method allows you to post above Arrange a Runnable object to the main thread in the queue,
sendMessage class methods, allowing you to arrange a Message object with the data to the queue,Spy Camera Glasses 4GB Fashionable Video Eyewear Recorder, wait for the update.
Handler Example
(1) sub-class needs to inherit Hendler class, and override handleMessage (Message msg) method for receiving data
The following is the thread an instance that implements the function: to modify the interface by thread Button’s content
public class MyHandlerActivity extends Activity {
Button button;
MyHandler myHandler;
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.handlertest);
button = (Button) findViewById (R.id.button);
myHandler = new MyHandler ();
/ / When you create a new Handler instance, it will bind to the current thread and message queues began to distribute the data
/ / Handler There are two effects, (1): Timing and Runnalbe Executive Message object
/ / (2): to make a move, in a different thread.
/ / it is arranged news in the following way
/ / post (Runnable)
/ / postAtTime (Runnable,couple watches, long)
/ / postDelayed (Runnable, long)
/ / sendEmptyMessage (int)
/ / sendMessage (Message);
/ / sendMessageAtTime (Message, long)
/ / sendMessageDelayed (Message,cheap bluetooth headset, long)
/ / the above method allows you to post the beginning of treatment Runnable object
/ / sendMessage () Message object allows you to handle (Message can contain data,)
MyThread m = new MyThread ();
new Thread (m). start ();
}
/ **
* receive messages, process messages, the Handler will be a main thread running with the current
* * /
class MyHandler extends Handler {
public MyHandler () {
}
public MyHandler (Looper L) {
super (L);
}
/ / Subclasses must override this method to receive data
@ Override
public void handleMessage (Message msg) {
/ / TODO Auto-generated method stub
Log.d (….br>
String color = b.getString (br>
class MyThread implements Runnable {
public void run () {
try {
Thread.sleep (10000);
} catch (InterruptedException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
Log.d (; thread …….new Bundle ();// store the data
b.putString (> MyHandlerActivity.this.myHandler.sendMessage (msg); / / Handler to send a message to update the UI
}
}
}