Andriod API之Handler
来源:互联网 发布:深圳淘宝网店推广培训 编辑:程序博客网 时间:2024/06/06 09:12
Handler
扩展了 ObjectClass Overview
Handler允许你通过一个线程的消息队列来发送、处理消息和可运行的对象。每个Handler实例与一个线程和线程的消息队列关联。当创建一个新的Handler,它属于创建它的那个线程,从此刻起,它将会传递消息并传送到消息队列,并当消息出列时执行他们。
Handler的两个主要用途:1.在当前线程存入消息;2.在其他线程出列处理消息
存储消息用post(Runnable)
, postAtTime(Runnable, long)
, postDelayed(Runnable, long)
, sendEmptyMessage(int)
,sendMessage(Message)
, sendMessageAtTime(Message, long)
, and sendMessageDelayed(Message, long)方法。
post版本允许你入队消息,当他们被接收到时被消息队列调用;sendMsg版本允许出列消息包含被handleMessage(Message)处理的数据(这将要求你实现Handler的子类)。
当post或send消息给Handler,你可以允许出列就被处理,或者当处理的时候有一个延迟。
当一个app创建一个进程,它的主线程运行一个处理管理高层应用对象(activities, broadcast receivers等)的消息队列。你可以创建自己的线程,通过Handler与主线程进行交互。
Summary
Looper
for the current thread.Looper
for the current thread and takes a callback interface in which you can handle messages.Looper
instead of the default one.Looper
instead of the default one and take a callback interface in which to handle messages.obtainMessage()
, except that it also sets the what, arg1 and arg2 members of the returned Message.Message
from the global message pool.obtainMessage()
, except that it also sets the what, obj, arg1,and arg2 values on the returned Message.obtainMessage()
, except that it also sets the what member of the returned Message.obtainMessage()
, except that it also sets the what and obj members of the returned Message.Public Constructors
public Handler ()
Default constructor associates this handler with the Looper
for the current thread. If this thread does not have a looper, this handler won't be able to receive messages so an exception is thrown.
public Handler (Handler.Callback callback)
Constructor associates this handler with the Looper
for the current thread and takes a callback interface in which you can handle messages. If this thread does not have a looper, this handler won't be able to receive messages so an exception is thrown.
Parameters
public Handler (Looper looper)
Use the provided Looper
instead of the default one.
Parameters
public Handler (Looper looper, Handler.Callback callback)
Use the provided Looper
instead of the default one and take a callback interface in which to handle messages.
Parameters
Public Methods
public void dispatchMessage (Message msg)
Handle system messages here.
public final void dump (Printer pw, String prefix)
public final Looper getLooper ()
public String getMessageName (Message message)
Returns a string representing the name of the specified message. The default implementation will either return the class name of the message callback if any, or the hexadecimal representation of the message "what" field.
Parameters
public void handleMessage (Message msg)
Subclasses must implement this to receive messages.
public 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.
public final boolean hasMessages (int what)
Check if there are any pending posts of messages with code 'what' in the message queue.
public 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.
Parameters
Returns
- A Message from the global message pool.
public final Message obtainMessage ()
Returns a new Message
from the global message pool. More efficient than creating and allocating new instances. The retrieved message has its handler set to this instance (Message.target == this). If you don't want that facility, just call Message.obtain() instead.
public 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.
Parameters
Returns
- A Message from the global message pool.
public final Message obtainMessage (int what)
Same as obtainMessage()
, except that it also sets the what member of the returned Message.
Parameters
Returns
- A Message from the global message pool.
public final Message obtainMessage (int what, Object obj)
Same as obtainMessage()
, except that it also sets the what and obj members of the returned Message.
Parameters
Returns
- A Message from the global message pool.
public final boolean post (Runnable r)
Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached.
Parameters
Returns
- Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
public final boolean postAtFrontOfQueue (Runnable r)
Posts a message to an object that implements Runnable. Causes the Runnable r to executed on the next iteration through the message queue. The runnable will be run on the thread to which this handler is attached. This method is only for use in very special circumstances -- it can easily starve the message queue, cause ordering problems, or have other unexpected side-effects.
Parameters
Returns
- Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
public 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. The time-base is uptimeMillis()
. The runnable will be run on the thread to which this handler is attached.
Parameters
uptimeMillis()
time-base.Returns
- Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting. Note that a result of true does not mean the Runnable will be processed -- if the looper is quit before the delivery time of the message occurs then the message will be dropped.
See Also
uptimeMillis()
public 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. The time-base is uptimeMillis()
. The runnable will be run on the thread to which this handler is attached.
Parameters
uptimeMillis()
time-base.Returns
- Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting. Note that a result of true does not mean the Runnable will be processed -- if the looper is quit before the delivery time of the message occurs then the message will be dropped.
public 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. The runnable will be run on the thread to which this handler is attached.
Parameters
Returns
- Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting. Note that a result of true does not mean the Runnable will be processed -- if the looper is quit before the delivery time of the message occurs then the message will be dropped.
public final void removeCallbacks (Runnable r)
Remove any pending posts of Runnable r that are in the message queue.
public final void removeCallbacks (Runnable r, Object token)
Remove any pending posts of Runnable r with Object token that are in the message queue. If token is null, all callbacks will be removed.
public final void removeCallbacksAndMessages (Object token)
Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed.
public final void removeMessages (int what)
Remove any pending posts of messages with code 'what' that are in the message queue.
public 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. If object is null, all messages will be removed.
public final boolean sendEmptyMessage (int what)
Sends a Message containing only the what value.
Returns
- Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
public final boolean sendEmptyMessageAtTime (int what, long uptimeMillis)
Sends a Message containing only the what value, to be delivered at a specific time.
Returns
- Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
See Also
sendMessageAtTime(android.os.Message, long)
public 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.
Returns
- Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
See Also
sendMessageDelayed(android.os.Message, long)
public final boolean sendMessage (Message msg)
Pushes a message onto the end of the message queue after all pending messages before the current time. It will be received in handleMessage(Message)
, in the thread attached to this handler.
Returns
- Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
public 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. You will receive it in handleMessage(Message)
, in the thread attached to this handler. This method is only for use in very special circumstances -- it can easily starve the message queue, cause ordering problems, or have other unexpected side-effects.
Returns
- Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
public boolean sendMessageAtTime (Message msg, long uptimeMillis)
Enqueue a message into the message queue after all pending messages before the absolute time (in milliseconds) uptimeMillis. The time-base is uptimeMillis()
. You will receive it in handleMessage(Message)
, in the thread attached to this handler.
Parameters
uptimeMillis()
time-base.Returns
- Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting. Note that a result of true does not mean the message will be processed -- if the looper is quit before the delivery time of the message occurs then the message will be dropped.
public final boolean sendMessageDelayed (Message msg, long delayMillis)
Enqueue a message into the message queue after all pending messages before (current time + delayMillis). You will receive it in handleMessage(Message)
, in the thread attached to this handler.
Returns
- Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting. Note that a result of true does not mean the message will be processed -- if the looper is quit before the delivery time of the message occurs then the message will be dropped.
public String toString ()
Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:
getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful toString
method if you intend implementing your own toString
method.
Returns
- a printable representation of this object.
- Andriod API之Handler
- Andriod----Handler
- Android API之Handler
- [Andriod官方API指南]连接之蓝牙
- [Andriod官方API指南]连接之蓝牙
- Andriod中Handler详解
- andriod Handler机制原理
- Andriod Handler更新UI
- Andriod Handler消息机制
- [Andriod]Andriod之HelloWorld
- Andriod的API
- andriod 使用隐藏API
- Api 25 Reference-->andriod
- Andriod中Handler,Looper,MessageQueue的关系
- Andriod Timer 及 Handler 线程间通信
- 《android framework常用api源码分析》之handler消息机制
- 《android framework常用api源码分析》之HandlerThread handler线程
- Andriod学习之路
- spring mvc 上传图片
- vim配置文件 .vimrc 。按照那篇将vim打造成IDE来的,成功加入各项功能,另外改善了书签不能保存的情况
- 安装Oracle 11g r2先决条件检查失败解决方法
- 6410平台上配置DDR的参数
- android 播放网络或本地gif格式的动态图片
- Andriod API之Handler
- 我的Android进阶之旅------>HTTP Header 详解
- 东风风神S30 1.5MT 模仿中进步
- Android中设置EditText默认无焦点
- Struts2 S标签 数目字格式化成金额输出(保留两位小数)
- XHR2-XMLHttpRequest
- 在marvel166平台上面调试DDR
- 【Tech-Android-Jni】Jni的Jclass JmethodID JfrieldID的差异
- FTP的使用和注意事项