Handler 【中文翻译小组】

Handler

Kotlin |Java

public class Handler extends Object 

 

java.lang.Object
   ↳android.os.Handler
Known direct subclasses

AsyncQueryHandlerAsyncQueryHandler.WorkerHandlerHttpAuthHandlerSslErrorHandler

 


A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue.

Handler 允许您发送和处理与线程的MessageQueue关联的Message和Runnable对象。

Each Handler instance is associated with a single thread and that thread's message queue.

每个Handler实例都与一个线程和该线程的消息队列关联。

When you create a new Handler,

创建新的Handler时,

it is bound to the thread / message queue of the thread that is creating it -- from that point on,

它将绑定到正在创建它的线程的线程/消息队列中-从那时起,

it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

它将把消息和可运行对象传递到该消息队列,并在它们从消息中出来时执行它们 队列。

 

There are two main uses for a Handler:

Handler有两个主要用途:

(1) to schedule messages and runnables to be executed at some point in the future;

(1)计划消息和可运行文件在将来某个时间执行;

and (2) to enqueue an action to be performed on a different thread than your own.

(2)将要在不同线程上执行的操作排队。

Scheduling messages is accomplished with the post(Runnable)postAtTime(java.lang.Runnable, long),postDelayed(Runnable, Object, long)sendEmptyMessage(int)sendMessage(Message)sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods.

调度消息是通过以下方法完成的:post(Runnable)、postAtTime(java.lang.Runnable,Long)、postDelayed(Runnable,Object,Long)、EsendEmptyMessage(Int)、EsendMessage(Message)、EsendMessageAtTime(message,Long)和EsendMessageDelayed(message,Long)。

 

The post versions allow you to enqueue Runnable objects to be called by the message queue when they are received;

POST消息版本允许您将接收到的可运行对象入队,以供消息队列调用Runnable objects;

the sendMessageversions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's handleMessage(Message) method (requiring that you implement a subclass of Handler).

 

EsendMessagversions允许您将包含将由Handler的handleMessage(Message)方法,处理的数据包的消息对象入队。

(要求您实现Handler的子类)。

 

When posting or sending to a Handler,

在发送或发送到处理程序时,

you can either allow the item to be processed as soon as the message queue is ready to do so,

您可以在消息队列准备好时立即处理该项,

or specify a delay before it gets processed or absolute time for it to be processed.

也可以指定处理该项之前的延迟或处理该项的绝对时间。
The latter two allow you to implement timeouts, ticks, and other timing-based behavior.

后两种方法允许您实现超时、计时和其他基于计时的行为。
 

 

When a process is created for your application,

为应用程序创建进程时,

its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create.

其主线程专用于运行消息队列,该队列负责管理顶级应用程序对象(活动、广播接收器等)及其创建的任何窗口。

You can create your own threads, and communicate back with the main application thread through a Handler.

您可以创建自己的线程,并通过处理程序与主应用程序线程进行通信。

This is done by calling the same post or sendMessage methods as before,

这是通过调用与以前相同的post或sendMessage方法来完成的,

but from your new thread.

但要从新线程调用。

The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.

给定的Runnable或消息将被安排在处理程序的消息队列中,并在适当时进行处理。

 

 

 

Summary

Nested classes

interfaceHandler.Callback

Callback interface you can use when instantiating a Handler

在实例化Handler时可以使用回调接口,

to avoid having to implement your own subclass of Handler. 

以避免必须实现自己的Handler子类。

Public constructors

Handler()

Default constructor associates this handler with the Looper for the current thread.

 

默认的构造器,将当前线程的 handler 与 Looper 相关联

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.

 

多了一个回调接口,用来处理回调的 messages

Handler(Looper looper)

Use the provided Looper instead of the default one.

 

用提供的 looper 代替默认的

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.

 

回调 looper 和 messages

Public methods

static HandlercreateAsync(Looper looper, Handler.Callback callback)

Create a new Handler whose posted messages and runnables are not subject to synchronization barriers such as display vsync.

 

创建一个新的处理程序, 其发布的 messages 和 runnables 不受同步屏障 (如显示 vsync) 的限制。

static HandlercreateAsync(Looper looper)

Create a new Handler whose posted messages and runnables are not subject to synchronization barriers such as display vsync.

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)

Subclasses must implement this to receive messages.

final booleanhasCallbacks(Runnable r)

Check if there are any pending posts of messages with callback r 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 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 MessageobtainMessage(int what, Object obj)

Same as obtainMessage(), except that it also sets the what and obj members of the returned Message.

final MessageobtainMessage()

Returns a new Message from the global message pool.

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(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 booleanpost(Runnable 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, 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, 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 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 booleanpostDelayed(Runnable r, Object token, 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 representation of the object.

Inherited methods

From class java.lang.Object

Public constructors

Handler

Added in API level 1

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.

 

Handler

Added in API level 3

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
callbackHandler.Callback: The callback interface in which to handle messages, or null. This value may be null.

 

Handler

Added in API level 1

public Handler (Looper looper)

Use the provided Looper instead of the default one.

 

Parameters
looperLooper: The looper, must not be null. This value must never be null.

 

Handler

Added in API level 3

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
looperLooper: The looper, must not be null. This value must never be null.

 

callbackHandler.Callback: The callback interface in which to handle messages, or null. This value may be null.

 

Public methods

createAsync

Added in API level 28

public static Handler createAsync (Looper looper, 
                Handler.Callback callback)

Create a new Handler whose posted messages and runnables are not subject to synchronization barriers such as display vsync.

Messages sent to an async handler are guaranteed to be ordered with respect to one another, but not necessarily with respect to messages from other Handlers.

 

 

Parameters
looperLooper: the Looper that the new Handler should be bound to This value must never be null.

 

callbackHandler.Callback: This value must never be null.

 

Returns
Handlera new async Handler instance This value will never be null.

 

See also:

createAsync

Added in API level 28

public static Handler createAsync (Looper looper)

Create a new Handler whose posted messages and runnables are not subject to synchronization barriers such as display vsync.

Messages sent to an async handler are guaranteed to be ordered with respect to one another, but not necessarily with respect to messages from other Handlers.

 

 

Parameters
looperLooper: the Looper that the new Handler should be bound to This value must never be null.

 

Returns
Handlera new async Handler instance This value will never be null.

 

See also:

dispatchMessage

Added in API level 1

public void dispatchMessage (Message msg)

Handle system messages here.

 

Parameters
msgMessage: This value must never be null.

 

dump

Added in API level 1

public final void dump (Printer pw, 
                String prefix)

 

 

Parameters
pwPrinter: This value must never be null.

 

prefixString: This value must never be null.

 

getLooper

Added in API level 1

public final Looper getLooper ()

 

 

Returns
LooperThis value will never be null.

 

getMessageName

Added in API level 14

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
messageMessage: The message whose name is being queried This value must never be null.

 

Returns
StringThis value will never be null.

 

handleMessage

Added in API level 1

public void handleMessage (Message msg)

Subclasses must implement this to receive messages.

 

Parameters
msgMessage: This value must never be null.

 

hasCallbacks

Added in API level 29

public final boolean hasCallbacks (Runnable r)

Check if there are any pending posts of messages with callback r in the message queue.

 

Parameters
rRunnable: This value must never be null.

 

Returns
boolean

 

hasMessages

Added in API level 1

public final boolean hasMessages (int what)

Check if there are any pending posts of messages with code 'what' in the message queue.

 

Parameters
whatint

 

Returns
boolean

 

hasMessages

Added in API level 1

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.

 

Parameters
whatint

 

objectObject: This value may be null.

 

Returns
boolean

 

obtainMessage

Added in API level 1

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
whatint: Value to assign to the returned Message.what field.

 

objObject: Value to assign to the returned Message.obj field. This value may be null.

 

Returns
MessageA Message from the global message pool. This value will never be null.

 

obtainMessage

Added in API level 1

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.

 

Returns
MessageThis value will never be null.

 

obtainMessage

Added in API level 1

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
whatint: Value to assign to the returned Message.what field.

 

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

 

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

 

Returns
MessageA Message from the global message pool. This value will never be null.

 

obtainMessage

Added in API level 1

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
whatint: Value to assign to the returned Message.what field.

 

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

 

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

 

objObject: Value to assign to the returned Message.obj field. This value may be null.

 

Returns
MessageA Message from the global message pool. This value will never be null.

 

obtainMessage

Added in API level 1

public final Message obtainMessage (int what)

Same as obtainMessage(), except that it also sets the what member of the returned Message.

 

Parameters
whatint: Value to assign to the returned Message.what field.

 

Returns
MessageA Message from the global message pool. This value will never be null.

 

post

Added in API level 1

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
rRunnable: The Runnable that will be executed. This value must never be null.

 

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

 

postAtFrontOfQueue

Added in API level 1

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
rRunnable: The Runnable that will be executed. This value must never be null.

 

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

 

postAtTime

Added in API level 1

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 SystemClock.uptimeMillis(). Time spent in deep sleep will add an additional delay to execution. The runnable will be run on the thread to which this handler is attached.

 

Parameters
rRunnable: The Runnable that will be executed. This value must never be null.

 

uptimeMillislong: The absolute time at which the callback should run, using the SystemClock.uptimeMillis() time-base.

 

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

 

postAtTime

Added in API level 1

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 SystemClock.uptimeMillis(). Time spent in deep sleep will add an additional delay to execution. The runnable will be run on the thread to which this handler is attached.

 

Parameters
rRunnable: The Runnable that will be executed. This value must never be null.

 

tokenObject: An instance which can be used to cancel r via removeCallbacksAndMessages(Object). This value may be null.

 

uptimeMillislong: The absolute time at which the callback should run, using the SystemClock.uptimeMillis() time-base.

 

Returns
booleanReturns 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:

postDelayed

Added in API level 1

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. The time-base is SystemClock.uptimeMillis().Time spent in deep sleep will add an additional delay to execution.

 

Parameters
rRunnable: The Runnable that will be executed. This value must never be null.

 

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

 

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

 

postDelayed

Added in API level 28

public final boolean postDelayed (Runnable r, 
                Object token, 
                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. The time-base is SystemClock.uptimeMillis().Time spent in deep sleep will add an additional delay to execution.

 

Parameters
rRunnable: The Runnable that will be executed. This value must never be null.

 

tokenObject: An instance which can be used to cancel r via removeCallbacksAndMessages(Object). This value may be null.

 

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

 

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

 

removeCallbacks

Added in API level 1

public final void removeCallbacks (Runnable r)

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

 

Parameters
rRunnable: This value must never be null.

 

removeCallbacks

Added in API level 1

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.

 

Parameters
rRunnable: This value must never be null.

 

tokenObject: This value may be null.

 

removeCallbacksAndMessages

Added in API level 1

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.

 

Parameters
tokenObject: This value may be null.

 

removeMessages

Added in API level 1

public final void removeMessages (int what)

Remove any pending posts of messages with code 'what' that are in the message queue.

 

Parameters
whatint

 

removeMessages

Added in API level 1

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.

 

Parameters
whatint

 

objectObject: This value may be null.

 

sendEmptyMessage

Added in API level 1

public final boolean sendEmptyMessage (int what)

Sends a Message containing only the what value.

 

Parameters
whatint

 

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

 

sendEmptyMessageAtTime

Added in API level 1

public final boolean sendEmptyMessageAtTime (int what, 
                long uptimeMillis)

Sends a Message containing only the what value, to be delivered at a specific time.

 

Parameters
whatint

 

uptimeMillislong

 

Returns
booleanReturns 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:

sendEmptyMessageDelayed

Added in API level 1

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.

 

Parameters
whatint

 

delayMillislong

 

Returns
booleanReturns 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:

sendMessage

Added in API level 1

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.

 

Parameters
msgMessage: This value must never be null.

 

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

 

sendMessageAtFrontOfQueue

Added in API level 1

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.

 

Parameters
msgMessage: This value must never be null.

 

Returns
booleanReturns 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

Added in API level 1

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 SystemClock.uptimeMillis(). Time spent in deep sleep will add an additional delay to execution. You will receive it in handleMessage(Message), in the thread attached to this handler.

 

Parameters
msgMessage: This value must never be null.

 

uptimeMillislong: The absolute time at which the message should be delivered, using the SystemClock.uptimeMillis() time-base.

 

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

 

sendMessageDelayed

Added in API level 1

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.

 

Parameters
msgMessage: This value must never be null.

 

delayMillislong

 

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

 

toString

Added in API level 1

public String toString ()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

 

 

 

Returns
Stringa string representation of the object.

 

此页内容对您有帮助吗?

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值