android Handler機制詳解

  簡單運行圖:
  
名詞解析:

 

 

Message(消息):定義了一個包含描述以及隨意的數據對象能夠被髮送到Hanlder的消息,得到消息的最好方法是Message.obtain或者Handler.obtainMessage方法;

MessageQueue (消息隊列):是looper中的一個消息隊列;html

Looper:用於使一個消息隊列在線程中循環,線程中默認是沒有消息隊列循環的,建立方法demo:
  classLooperThreadextendsThread{
     
publicHandler mHandler;

     
publicvoid run(){
         
Looper.prepare();

          mHandler
=newHandler(){
             
publicvoid handleMessage(Message msg){
                 
// process incoming messages here
             
}
         
};

         
Looper.loop();
     
}
 
}
 
Looper方法
void dump(Printer pw, String prefix)
synchronized static Looper getMainLooper()
  得到主線程的Looper
Returns the application's main looper, which lives in the main thread of the application.
Thread getThread()
Return the Thread associated with this Looper.
static void loop()
Run the message queue in this thread.
static Looper myLooper()
得到當前線程關聯的Looper
Return the Looper object associated with the current thread.
static MessageQueue myQueue()
得到當前線程關聯的消息隊列
Return the  MessageQueue object associated with the current thread.
static void prepare()
Initialize the current thread as a looper.
static void prepareMainLooper()
Initialize the current thread as a looper, marking it as an application's main looper.
 


Handler(消息控制器):一個處理程序容許您發送或者處理消息以及與線程的MessageQueue相關聯的可執行對象,每一個Handler實例與單個線程以及該線程的消息隊列想關聯。當你建立一個新的Handler 實例的時候,該實例將被綁定到線程消息隊列,該實例將傳遞messages消息或者可運行的對象到   message queue消息隊列而且在當消息被獲取的時候執行他們。
    Handler的兩個主要用途:
  •  安排消息或者可執行對象在將來的某個時間點執行。
  • 將一個在子線程執行的動做放到消息隊列中
處理消息經過  post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int),sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long)  方法來完成。post系列方法容許你隊列一個可執行對象,當消息被handler所在線程接收的時候會執行;sendMessage容許你隊列一個包含一捆數據的Message對象,到消息handler所在線程接收的時候將調用handler實例的handleMessage(Message)方法,Massage 就是在另外一個線程發送的消息。
    當你使用handler發送消息或者可執行對象的時候你能夠選擇handler所在線程得到消息就執行或延遲必定的時間再執行,當應用程序建立一個線程的時候,它的主線程專門運行一個 message queue   消息隊列用於維護應用程序頂級組件 。咱們能夠建立子線程經過Handler實例與主線程通訊。
 
Handler 方法介紹
void dispatchMessage(Message msg)
Handle system messages here.
final void dump(Printer pw, String prefix)
final Looper getLooper()
String getMessageName(Message message)
Returns a string representing the name of the specified message.
void handleMessage(Message msg)
  子類必須實現這個方法來接收消息
final boolean hasMessages(int what, Object object)
Check if there are any pending posts of messages with code 'what' and whose obj is 'object' in the message queue.
final boolean hasMessages(int what)
Check if there are any pending posts of messages with code 'what' in the message queue.
final Message obtainMessage(int what, int arg1, int arg2)
Same as  obtainMessage(), except that it also sets the what, arg1 and arg2 members of the returned Message.
final Message obtainMessage()
Returns a new  Message from the global message pool.
final Message obtainMessage(int what, int arg1, int arg2, Object obj)
Same as  obtainMessage(), except that it also sets the what, obj, arg1,and arg2 values on the returned Message.
final Message obtainMessage(int what)
Same as  obtainMessage(), except that it also sets the what member of the returned Message.
final Message obtainMessage(int what, Object obj)
Same as  obtainMessage(), except that it also sets the what and obj members of the returned Message.
final boolean post(Runnable r)
   使可執行的r被添加到消息隊列
Causes the Runnable r to be added to the message queue.
final boolean postAtFrontOfQueue(Runnable r)
Posts a message to an object that implements Runnable.
final boolean postAtTime(Runnable r, Object token, long uptimeMillis)
    在規定的時間點執行
Causes the Runnable r to be added to the message queue, to be run at a specific time given by  uptimeMillis.
final boolean postAtTime(Runnable r, long uptimeMillis)
Causes the Runnable r to be added to the message queue, to be run at a specific time given by  uptimeMillis.
final boolean postDelayed(Runnable r, long delayMillis)
延遲執行規定的時間長度
Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses.
final void removeCallbacks(Runnable r)
Remove any pending posts of Runnable r that are in the message queue.
final void removeCallbacks(Runnable r, Object token)
Remove any pending posts of Runnable  r with Object  token that are in the message queue.
final void removeCallbacksAndMessages(Object token)
Remove any pending posts of callbacks and sent messages whose  obj is  token.
final void removeMessages(int what)
Remove any pending posts of messages with code 'what' that are in the message queue.
final void removeMessages(int what, Object object)
Remove any pending posts of messages with code 'what' and whose obj is 'object' that are in the message queue.
final boolean sendEmptyMessage(int what)
Sends a Message containing only the what value.
final boolean sendEmptyMessageAtTime(int what, long uptimeMillis)
Sends a Message containing only the what value, to be delivered at a specific time.
final boolean sendEmptyMessageDelayed(int what, long delayMillis)
Sends a Message containing only the what value, to be delivered after the specified amount of time elapses.
final boolean sendMessage(Message msg)
將一個消息添加到消息隊列的末尾
Pushes a message onto the end of the message queue after all pending messages before the current time.
final boolean sendMessageAtFrontOfQueue(Message msg)
將一個消息添加到消息隊列的最前面
Enqueue a message at the front of the message queue, to be processed on the next iteration of the message loop.
boolean sendMessageAtTime(Message msg, long uptimeMillis)
在規定的時間點添加消息到隊列
Enqueue a message into the message queue after all pending messages before the absolute time (in milliseconds)  uptimeMillis.
final boolean sendMessageDelayed(Message msg, long delayMillis)
延遲規定的時間再消息添加到消息隊列
Enqueue a message into the message queue after all pending messages before (current time + delayMillis).
String toString()
Returns a string containing a concise, human-readable description of this object.
 
接觸不深,請高手指點,持續更新!
相關文章
相關標籤/搜索