android.os.handler related knowledge finishing
Handler is responsible for sending and processing messages in Android . Its main uses are:
Some methods of distributing messages in Handler
post(Runnable)
postAtTime(Runnable, long)
postDelayed(Runnable long)
sendEmptyMessage(int)
sendMessage(Message)
sendMessageAtTime(Message, long)
sendMessageDelayed(Message, long) The
above post class methods allow you to arrange a Runnable object to the main thread queue ,
sendMessage class method , allows you to arrange a Message object with data to the queue, waiting to be updated .
When the application starts, Android will first start a main thread ( that is, the UI thread ). The main thread is the UI control in the management interface , which distributes events . For example , if you click a Button, Android will distribute events to the Button . , in response to your action. If you need a time-consuming operation at this time, such as : reading data online, or reading a large local file, you can't put these operations in the main thread, if you put it in the main thread, The interface will appear suspended animation . If it is not completed in 5 seconds, you will receive an error prompt " Forced Close " from the Android system. At this time, we need to put these time-consuming operations in a child thread , because the child thread When it comes to UI updates, the Android main thread is thread-unsafe, that is, updating the UI can only be updated in the main thread, and the operation in the sub-thread is dangerous . At this time,Since the Handler runs in the main thread (in the UI thread ), it and the child thread can pass data through the Message object . At this time, the Handler is responsible for accepting the Message object ( which contains data ) passed by the child thread, and puts these messages in the Enter the main thread queue, and cooperate with the main thread to update the UI .