UUTON

悠悠途恩

Android Handler定义

<一> Handler的定义:


          作用:主要接受子线程发送的数据, 并用此数据配合主线程更新UI.

          解释: 当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件,进行事件分发, 比如用户点击一个 Button ,Android会分发事件到Button上,来响应用户操作。  若此时需要一个耗时操作,如:联网读取数据或读取本地较大文件时,这些操作不能放在主线程中,若放在主线程中,界面会出现假死现象, 如果5秒钟还没有完成,会收到Android系统错误提示  "强制关闭".  这个时候我们需要把这些耗时的操作,放在一个子线程中,而Android主线程是线程不安全的,也就是说,更新UI只能在主线程中更新.这个时候,Handler就出现了,来解决这个复杂的问题 ,    由于Handler运行在主线程中(UI线程中),  它与子线程可以通过Message对象来传递数据, 这个时候,Handler就承担着接受子线程传过来的 (子线程用sedMessage()方法传递) Message对象(里面包含数据)  , 把这些消息放入主线程队列中,配合主线程进行更新UI。

<二> Handler一些特点:

 

        当创建一个新的Handler实例时, 它会绑定到当前线程和消息的队列中,开始分发数据

        Handler有两个作用: 1) : 定时执行Message和Runnalbe 对象  2)  : 让一个动作,在不同的线程中执行.


<二> Handler一些方法:

       它安排消息,用以下方法

public final boolean post (Runnable r) 

将Runnable对象r加入消息队列。该Runnable对象将被运行在handler所依附的线程中。如果成功加入了消息队列返回true,否则返回false,返回false经常是因为looper处理在消息中已经存在。

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. 

返回值

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, long uptimeMillis) 另一同名方法参数为(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

r  The Runnable that will be executed. 

uptimeMillis  The absolute time at which the callback should run, using the 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 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

r  The Runnable that will be executed. 

delayMillis  The delay (in milliseconds) until the Runnable will be executed. 


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, Object token) 

Remove any pending posts of Runnable r with Object token that are in the message queue. 


public final void removeCallbacks (Runnable r) 

Remove any pending posts of Runnable r that are in the message queue. 

移除消息队列中,所有等待处理的Runnable对象r,r对象并没有销毁,只是将r的引用从消息队列里拿出来。


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 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. 


同理:    sendMessageAtTime(Message,long)

        sendMessageDelayed(Message,long)       


        以上方法以 post开头的允许你处理Runnable对象,sendMessage()允许你处理Message对象(Message里可以包含数据,)


public final Message obtainMessage (int what, int arg1, int arg2, Object obj) 

Same as 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.  except that it also sets the what, obj, arg1,and arg2 values on the returned Message.


Parameters

what  Value to assign to the returned Message.what field. 

arg1  Value to assign to the returned Message.arg1 field. 

arg2  Value to assign to the returned Message.arg2 field. 

obj  Value to assign to the returned Message.obj field. 


Returns

A Message from the global message pool. 


评论

© UUTON | Powered by LOFTER