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。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM