android.os.handler

android.os.handler related knowledge finishing

 

Handler is responsible for sending and processing messages in Android . Its main uses are:

  1) Send a message on a schedule or execute a Runnanble (using the POST method);
  2) Messages sent from other threads are put into the message queue to avoid thread conflicts (commonly used to update UI threads) / used to add an action to a queue that does not belong to its own thread
 

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 .

   By default, Handler accepts the message loop instance under the current thread (using Handler ( Looper  looper), Handler ( Looper  looper,  Handler.Callback  callback) can specify the thread), and a message queue can be used by multiple Objects are distributed and processed (in the UI thread, the system already has an Activity to process, you can start several Handlers to process). When instantiating Handler, Looper can be of any thread, and any thread can sendMessage as long as there is a pointer to Handler. Handler's processing of Message is not concurrent. A Looper will read the next message only after processing a message, so the processing of the message is in blocking form (there should be no time-consuming operation in the handleMessage() method, the time-consuming operation can be executed in other threads, and sent after the operation is completed Message (through the sendMessages method), and then update the UI by handleMessage().
 

      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 .

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326811150&siteId=291194637