首页 > 代码库 > android Handler机制详解

android Handler机制详解

  简单运行图:
  
名词解析:

 

 

Message(消息):定义了一个包含描述以及随意的数据对象可以被发送到Hanlder的消息,获得消息的最好方法是Message.obtain或者Handler.obtainMessage方法;

MessageQueue (消息队列):是looper中的一个消息队列;

Looper:用于使一个消息队列在线程中循环,线程中默认是没有消息队列循环的,创建方法demo:
  classLooperThreadextendsThread{
     
publicHandler mHandler;

     
publicvoid run(){
         
Looper.prepare();

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

         
Looper.loop();
     
}
 
}
 
Looper方法
voiddump(Printer pw, String prefix)
synchronized static LoopergetMainLooper()
  获得主线程的Looper
Returns the application‘s main looper, which lives in the main thread of the application.
ThreadgetThread()
Return the Thread associated with this Looper.
static voidloop()
Run the message queue in this thread.
static LoopermyLooper()
获得当前线程关联的Looper
Return the Looper object associated with the current thread.
static MessageQueuemyQueue()
获得当前线程关联的消息队列
Return the MessageQueue object associated with the current thread.
static voidprepare()
Initialize the current thread as a looper.
static voidprepareMainLooper()
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 方法介绍
voiddispatchMessage(Message msg)
Handle system messages here.
final voiddump(Printer pw, String prefix)
final LoopergetLooper()
StringgetMessageName(Message message)
Returns a string representing the name of the specified message.
voidhandleMessage(Message msg)
  子类必须实现这个方法来接收消息
final booleanhasMessages(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 booleanhasMessages(int what)
Check if there are any pending posts of messages with code ‘what‘ in the message queue.
final MessageobtainMessage(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 MessageobtainMessage()
Returns a new Message from the global message pool.
final MessageobtainMessage(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 MessageobtainMessage(int what)
Same as obtainMessage(), except that it also sets the what member of the returned Message.
final MessageobtainMessage(int what, Object obj)
Same as obtainMessage(), except that it also sets the what and obj members of the returned Message.
final booleanpost(Runnable r)
   使可执行的r被添加到消息队列
Causes the Runnable r to be added to the message queue.
final booleanpostAtFrontOfQueue(Runnable r)
Posts a message to an object that implements Runnable.
final booleanpostAtTime(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 booleanpostAtTime(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 booleanpostDelayed(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 voidremoveCallbacks(Runnable r)
Remove any pending posts of Runnable r that are in the message queue.
final voidremoveCallbacks(Runnable r, Object token)
Remove any pending posts of Runnable r with Object token that are in the message queue.
final voidremoveCallbacksAndMessages(Object token)
Remove any pending posts of callbacks and sent messages whose obj is token.
final voidremoveMessages(int what)
Remove any pending posts of messages with code ‘what‘ that are in the message queue.
final voidremoveMessages(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 booleansendEmptyMessage(int what)
Sends a Message containing only the what value.
final booleansendEmptyMessageAtTime(int what, long uptimeMillis)
Sends a Message containing only the what value, to be delivered at a specific time.
final booleansendEmptyMessageDelayed(int what, long delayMillis)
Sends a Message containing only the what value, to be delivered after the specified amount of time elapses.
final booleansendMessage(Message msg)
将一个消息添加到消息队列的末尾
Pushes a message onto the end of the message queue after all pending messages before the current time.
final booleansendMessageAtFrontOfQueue(Message msg)
将一个消息添加到消息队列的最前面
Enqueue a message at the front of the message queue, to be processed on the next iteration of the message loop.
booleansendMessageAtTime(Message msg, long uptimeMillis)
在规定的时间点添加消息到队列
Enqueue a message into the message queue after all pending messages before the absolute time (in milliseconds) uptimeMillis.
final booleansendMessageDelayed(Message msg, long delayMillis)
延迟规定的时间再消息添加到消息队列
Enqueue a message into the message queue after all pending messages before (current time + delayMillis).
StringtoString()
Returns a string containing a concise, human-readable description of this object.
 
接触不深,请高手指点,持续更新!
 
 
 
 
 
 
 

 



android Handler机制详解