Message类介绍(中文文档)

public final class

Message

extends Object

implements Parcelable

java.lang.Object

     android.os.Message

Class Overview(类概述)

Defines a message containing a description and arbitrary data object that can be sent to a Handler. This object contains two extra int fields and an extra object field that allow you to not do allocations in many cases.

定义一个包含任意类型的描述数据对象,此对象可以发送给Handler。对象包含两个额外的int字段和一个额外的对象字段,这样可以使得在很多情况下不用做分配工作。

While the constructor of Message is public, the best way to get one of these is to call Message.obtain() or one of the Handler.obtainMessage() methods, which will pull them from a pool of recycled objects.

尽管Message的构造器是公开的,但是获取Message对象的最好方法是调用Message.obtain()或者Handler.obtainMessage(), 这样是从一个可回收对象池中获取Message对象。

Summary(摘要)

Inherited Constants(继承常量)

From interface(接口) android.os.Parcelable

Fields(字段)

public static final Creator<Message>

CREATOR

public int

arg1

arg1 and arg2 are lower-cost alternatives to using setData() if you only need to store a few integer values.

如果只需要存储一些整型值,arg1 和 arg2是用setData()的低成本替代品。

public int

arg2

arg1 and arg2 are lower-cost alternatives to using setData() if you only need to store a few integer values.

如果只需要存储一些整型值,arg1 和 arg2是用setData()的低成本替代品。

public Object

obj

An arbitrary object to send to the recipient. When using Messenger to send the message across processes this can only be non-null if it contains a Parcelable of a framework class (not one implemented by the application). For other data transfer use setData(Bundle).

发送给接收者的任意对象。当使用Message对象在线程间传递消息时,如果它包含一个Parcelable的结构类(不是由应用程序实现的类),此字段必须为非空(non-null)。其他的数据传输则使用setData(Bundle)方法。

Note that Parcelable objects here are not supported prior to the FROYO release.

注意Parcelable对象是从FROYO版本以后才开始支持的。

public Messenger

replyTo

Optional Messenger where replies to this message can be sent. The semantics of exactly how this is used are up to the sender and receiver.

指明此message发送到何处的可选Messenger对象。具体的使用方法由发送者和接受者决定。

public int

what

User-defined message code so that the recipient can identify what this message is about. Each Handler has its own name-space for message codes, so you do not need to worry about yours conflicting with other handlers.

用户自定义的消息代码,这样接受者可以确定这个消息的信息。每个handler各自包含自己的消息代码,所以不用担心自定义的消息跟其他handlers有冲突。

Public Constructors(公共构造函数)

Message()

Constructor (but the preferred way to get a Message is to call Message.obtain()).

构造器(但是获取Message对象的最好方法是调用Message.obtain())。

Public Methods(公共方法)

void

copyFrom(Message o)

Make this message like o. Performs a shallow copy of the data field. Does not copy the linked list fields, nor the timestamp or target/callback of the original message.

使此message跟参数o相似。浅拷贝数据域。不拷贝源message的链表字段,时间戳和目标/回调。

int

describeContents()

Describe the kinds of special objects contained in this Parcelable's marshalled representation.

描述了包含在Parcelable对象排列信息中的特殊对象的类型。

Returns(返回值)

a bitmask indicating the set of special object types marshalled by the Parcelable.

一个位掩码,表明Parcelable对象特殊对象类型集合的排列。

Runnable

getCallback()

Retrieve callback object that will execute when this message is handled. This object must implement Runnable. This is called by the target Handler that is receiving this Message to dispatch it. If not set, the message will be dispatched to the receiving Handler's handleMessage(Message).

获取回调对象,此对象会在message处理时执行。此对象必须实现Runnable接口。回调由接收此消息并分发的目标handler调用。如果没有设置回调,此消息会分发到接收handler的

handleMessage(Message)。

Bundle

getData()

Obtains a Bundle of arbitrary data associated with this event, lazily creating it if necessary. Set this value by calling setData(Bundle). Note that when transferring data across processes via Messenger, you will need to set your ClassLoader on the Bundle via Bundle.setClassLoader() so that it can instantiate your objects when you retrieve them.

获取附加在此事件上的任意数据的Bundle对象,需要时延迟创建。通过调用setData(Bundle)来设置Bundle的值。需要注意的是,如果通过Messenger对象在进程间传递数据时,需要调用Bundle类的Bundle.setClassLoader()方法来设置ClassLoader,这样当接收到消息时可以实例化Bundle里的对象。

请参见

  peekData()

  setData(Bundle)

Handler

getTarget()

Retrieve the a Handler implementation that will receive this message. The object must implement Handler.handleMessage(). Each Handler has its own name-space for message codes, so you do not need to worry about yours conflicting with other handlers.

获取将接收此消息的Handler对象。此对象必须要实现Handler.handleMessage()方法。每个handler各自包含自己的消息代码,所以不用担心自定义的消息跟其他handlers相冲突。

long

getWhen()

Return the targeted delivery time of this message, in milliseconds.

返回此消息的传输时间,以毫秒为单位。

static Message

obtain(Handler h, int what, int arg1, int arg2, Object obj)

Same as obtain(), but sets the values of the target, what, arg1, arg2, and obj members.

与obtain()一样,但是设置了target, what, arg1, arg2和obj的值。

Parameters(参数)

h      The target value to set.    设置的target值

what  The what value to set.      设置的what值

arg1  The arg1 value to set.      设置的arg1值

arg2  The arg2 value to set.      设置的arg2值

obj        The obj value to set.       设置的obj值

Returns(返回值)

A Message object from the global pool.

从全局池中分配的一个Message对象。

static Message

obtain(Handler h, int what, Object obj)

Same as obtain(), but sets the values of the target, what, and obj members.

与obtain()一样,但是设置了target, what和obj的值。

Parameters(参数)

h       The target value to set.    设置的target值

what    The what value to set.      设置的what值

obj        The object method to set.   设置的obj值

Returns(返回值)

A Message object from the global pool.

从全局池中分配的一个Message对象。

static Message

obtain(Handler h, int what)

Same as obtain(), but sets the values for both target and what members on the Message. 与obtain()一样,但是设置了target和what的值。

Parameters(参数)

h       The target value to set.    设置的target值

what    The what value to set.      设置的what值

Returns(返回值)

A Message object from the global pool.

从全局池中分配的一个Message对象。

static Message

obtain(Handler h, Runnable callback)

Same as obtain(Handler), but assigns a callback Runnable on the Message that is returned.

与obtain(Handler)一样,但是设置回调函数,在Message返回时调用。

Parameters(参数)

h          Handler to assign to the returned Message object's target member.

            消息对象的target成员的值

callback Runnable that will execute when the message is handled.

            当消息处理时会调用的回调函数

Returns(返回值)

A Message object from the global pool.

从全局池中分配的一个Message对象。

static Message

obtain()

Return a new Message instance from the global pool. Allows us to avoid allocating new objects in many cases.

从全局池中返回的一个新的Message对象。避免在许多情况下,分配新的对象。

static Message

obtain(Handler h, int what, int arg1, int arg2)

Same as obtain(), but sets the values of the target, what, arg1, and arg2 members.

与obtain()一样,但是设置了target, what, arg1和arg2的值

Parameters(参数)

h       The target value to set.    设置的targe值

what    The what value to set.      设置的what值

arg1    The arg1 value to set.      设置的arg1值

arg2    The arg2 value to set.      设置的arg2值

Returns(返回值)

A Message object from the global pool.

从全局池中分配的一个Message对象。

static Message

obtain(Message orig)

Same as obtain(), but copies the values of an existing message (including its target) into the new one.

与obtain()一样,但是从一个已存在的消息中拷贝值(包括它的目标)。

Parameters(参数)

orig  Original message to copy.   要拷贝的源消息

Returns(返回值)

A Message object from the global pool.从全局池中分配的一个Message对象。

Bundle

peekData()

Like getData(), but does not lazily create the Bundle. A null is returned if the Bundle does not already exist. See getData() for further information on this.

与getData()相似,但是并不延迟创建Bundle。如果Bundle对象不存在返回null。更多信息见getData()。

See Also(请参见)

  getData()

  setData(Bundle)

void

recycle()

Return a Message instance to the global pool. You MUST NOT touch the Message after calling this function --- it has effectively been freed.

向全局池中返回一个Message实例。一定不能在调用此函数后再使用Message---它会立即被释放。

void

sendToTarget()

Sends this Message to the Handler specified by getTarget(). Throws a null pointer exception if this field has not been set.

向Handler发送此消息,getTarget()方法可以获取此Handler。如果这个字段没有设置会抛出空指针异常。

void

setData(Bundle data)

Sets a Bundle of arbitrary data values. Use arg1 and arg2 members as a lower cost way to send a few simple integer values, if you can.

设置一个任意数据值的Bundle对象。如果可以,使用arg1和arg2域发送一些整型值以减少消耗。

See Also(请参见)

getData()

peekData()

void

setTarget(Handler target)

设置将接收此消息的Handler对象。

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:

返回一个Message对象简单的,可读懂的描述信息。鼓励子类重写此方法,实现时最好把对象的类型的数据考虑进去。默认的实现等同于以下表达式:

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

See Writing a useful toString method if you intend implementing your own toString method.

如果你需要实现自己toString方法,参考Writing a useful toString method。

Returns(返回值)

a printable representation of this object.

一个代表此对象的可打印字符串

void

writeToParcel(Parcel dest, int flags)

Flatten this object in to a Parcel.

将类的数据写入外部提供的Parcel中

Parameters(参数)

dest  The Parcel in which the object should be written. 对象被写入的Parcel

flags Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.

对象如何被写入的附加标志,可能是0或PARCELABLE_WRITE_RETURN_VALUE。

Inherited Methods(继承方法)

From interface android.os.Parcelable

abstract int

describeContents()

Describe the kinds of special objects contained in this Parcelable's marshalled representation.

描述了包含在Parcelable对象排列信息中的特殊对象的类型。

Returns(返回值)

a bitmask indicating the set of special object types marshalled by the Parcelable.

一个位掩码,表明Parcelable对象特殊对象类型集合的排列。

abstract void

writeToParcel(Parcel dest, int flags)

Flatten this object in to a Parcel.

将类的数据写入外部提供的Parcel中

Parameters(参数)

dest  The Parcel in which the object should be written. 对象被写入的Parcel

flags Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.

对象如何被写入的附加标志,可能是0或PARCELABLE_WRITE_RETURN_VALUE。

 

posted on 2012-10-16 20:20  Harvey Ren  阅读(6023)  评论(0编辑  收藏  举报

导航