android Handler机制详解
MessageQueue (消息队列):是looper中的一个消息队列;
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 方法介绍 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
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.
|
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。