Class Message
- java.lang.Object
-
- android.os.Message
-
- All Implemented Interfaces:
Parcelable
public final class Message extends Object implements Parcelable
Defines a message containing a description and arbitrary data object that can be sent to aHandler. This object contains two extra int fields and an extra object field that allow you to not do allocations in many cases.While the constructor of Message is public, the best way to get one of these is to call
Message.obtain()or one of theHandler.obtainMessage()methods, which will pull them from a pool of recycled objects.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface android.os.Parcelable
Parcelable.ClassLoaderCreator<T>, Parcelable.Creator<T>
-
-
Field Summary
Fields Modifier and Type Field Description intarg1arg1 and arg2 are lower-cost alternatives to usingsetData()if you only need to store a few integer values.intarg2arg1 and arg2 are lower-cost alternatives to usingsetData()if you only need to store a few integer values.static Parcelable.Creator<Message>CREATORObjectobjAn arbitrary object to send to the recipient.MessengerreplyToOptional Messenger where replies to this message can be sent.intsendingUidOptional field indicating the uid that sent the message.intwhatUser-defined message code so that the recipient can identify what this message is about.-
Fields inherited from interface android.os.Parcelable
CONTENTS_FILE_DESCRIPTOR, PARCELABLE_WRITE_RETURN_VALUE
-
-
Constructor Summary
Constructors Constructor Description Message()Constructor (but the preferred way to get a Message is to callMessage.obtain()).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcopyFrom(Message o)Make this message like o.intdescribeContents()Describe the kinds of special objects contained in this Parcelable's marshalled representation.RunnablegetCallback()Retrieve callback object that will execute when this message is handled.BundlegetData()Obtains a Bundle of arbitrary data associated with this event, lazily creating it if necessary.HandlergetTarget()Retrieve the aHandlerimplementation that will receive this message.longgetWhen()Return the targeted delivery time of this message, in milliseconds.booleanisAsynchronous()Returns true if the message is asynchronous, meaning that it is not subject toLoopersynchronization barriers.static Messageobtain()Return a new Message instance from the global pool.static Messageobtain(Handler h)Same asobtain(), but sets the value for the target member on the Message returned.static Messageobtain(Handler h, int what)Same asobtain(), but sets the values for both target and what members on the Message.static Messageobtain(Handler h, int what, int arg1, int arg2)Same asobtain(), but sets the values of the target, what, arg1, and arg2 members.static Messageobtain(Handler h, int what, int arg1, int arg2, Object obj)Same asobtain(), but sets the values of the target, what, arg1, arg2, and obj members.static Messageobtain(Handler h, int what, Object obj)Same asobtain(), but sets the values of the target, what, and obj members.static Messageobtain(Handler h, Runnable callback)Same asobtain(Handler), but assigns a callback Runnable on the Message that is returned.static Messageobtain(Message orig)Same asobtain(), but copies the values of an existing message (including its target) into the new one.BundlepeekData()Like getData(), but does not lazily create the Bundle.voidrecycle()voidsendToTarget()Sends this Message to the Handler specified bygetTarget().voidsetAsynchronous(boolean async)Sets whether the message is asynchronous, meaning that it is not subject toLoopersynchronization barriers.voidsetData(Bundle data)Sets a Bundle of arbitrary data values.voidsetTarget(Handler target)StringtoString()Returns a string containing a concise, human-readable description of this object.voidwriteToParcel(Parcel dest, int flags)Flatten this object in to a Parcel.
-
-
-
Field Detail
-
what
public int what
User-defined message code so that the recipient can identify what this message is about. EachHandlerhas its own name-space for message codes, so you do not need to worry about yours conflicting with other handlers.
-
arg1
public int arg1
arg1 and arg2 are lower-cost alternatives to usingsetData()if you only need to store a few integer values.
-
arg2
public int arg2
arg1 and arg2 are lower-cost alternatives to usingsetData()if you only need to store a few integer values.
-
obj
public Object obj
An arbitrary object to send to the recipient. When usingMessengerto 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 usesetData(android.os.Bundle).Note that Parcelable objects here are not supported prior to the
android.os.Build.VERSION_CODES#FROYOrelease.
-
replyTo
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.
-
sendingUid
public int sendingUid
Optional field indicating the uid that sent the message. This is only valid for messages posted by aMessenger; otherwise, it will be -1.
-
CREATOR
public static final Parcelable.Creator<Message> CREATOR
-
-
Constructor Detail
-
Message
public Message()
Constructor (but the preferred way to get a Message is to callMessage.obtain()).
-
-
Method Detail
-
obtain
public static Message obtain()
Return a new Message instance from the global pool. Allows us to avoid allocating new objects in many cases.
-
obtain
public static Message obtain(Message orig)
Same asobtain(), but copies the values of an existing message (including its target) into the new one.- Parameters:
orig- Original message to copy.- Returns:
- A Message object from the global pool.
-
obtain
public static Message obtain(Handler h)
Same asobtain(), but sets the value for the target member on the Message returned.- Parameters:
h- Handler to assign to the returned Message object's target member.- Returns:
- A Message object from the global pool.
-
obtain
public static Message obtain(Handler h, Runnable callback)
Same asobtain(Handler), but assigns a callback Runnable on the Message that is returned.- Parameters:
h- Handler to assign to the returned Message object's target member.callback- Runnable that will execute when the message is handled.- Returns:
- A Message object from the global pool.
-
obtain
public static Message obtain(Handler h, int what)
Same asobtain(), but sets the values for both target and what members on the Message.- Parameters:
h- Value to assign to the target member.what- Value to assign to the what member.- Returns:
- A Message object from the global pool.
-
obtain
public static Message obtain(Handler h, int what, Object obj)
Same asobtain(), but sets the values of the target, what, and obj members.- Parameters:
h- The target value to set.what- The what value to set.obj- The object method to set.- Returns:
- A Message object from the global pool.
-
obtain
public static Message obtain(Handler h, int what, int arg1, int arg2)
Same asobtain(), but sets the values of the target, what, arg1, and arg2 members.- Parameters:
h- The target value to set.what- The what value to set.arg1- The arg1 value to set.arg2- The arg2 value to set.- Returns:
- A Message object from the global pool.
-
obtain
public static Message obtain(Handler h, int what, int arg1, int arg2, Object obj)
Same asobtain(), but sets the values of the target, what, arg1, arg2, and obj members.- Parameters:
h- The target value to set.what- The what value to set.arg1- The arg1 value to set.arg2- The arg2 value to set.obj- The obj value to set.- Returns:
- A Message object from the global pool.
-
recycle
public void recycle()
-
copyFrom
public 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.
-
getWhen
public long getWhen()
Return the targeted delivery time of this message, in milliseconds.
-
setTarget
public void setTarget(Handler target)
-
getTarget
public Handler getTarget()
Retrieve the aHandlerimplementation that will receive this message. The object must implementHandler.handleMessage(). Each Handler has its own name-space for message codes, so you do not need to worry about yours conflicting with other handlers.
-
getCallback
public Runnable getCallback()
Retrieve callback object that will execute when this message is handled. This object must implement Runnable. This is called by the targetHandlerthat is receiving this Message to dispatch it. If not set, the message will be dispatched to the receiving Handler's {@link Handler#handleMessage(Message Handler.handleMessage())}.
-
getData
public Bundle getData()
Obtains a Bundle of arbitrary data associated with this event, lazily creating it if necessary. Set this value by callingsetData(Bundle). Note that when transferring data across processes viaMessenger, you will need to set your ClassLoader on the Bundle viaBundle.setClassLoader()so that it can instantiate your objects when you retrieve them.- See Also:
peekData(),setData(Bundle)
-
peekData
public Bundle peekData()
Like getData(), but does not lazily create the Bundle. A null is returned if the Bundle does not already exist. SeegetData()for further information on this.- See Also:
getData(),setData(Bundle)
-
setData
public 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.- See Also:
getData(),peekData()
-
sendToTarget
public void sendToTarget()
Sends this Message to the Handler specified bygetTarget(). Throws a null pointer exception if this field has not been set.
-
isAsynchronous
public boolean isAsynchronous()
Returns true if the message is asynchronous, meaning that it is not subject toLoopersynchronization barriers.- Returns:
- True if the message is asynchronous.
- See Also:
setAsynchronous(boolean)
-
setAsynchronous
public void setAsynchronous(boolean async)
Sets whether the message is asynchronous, meaning that it is not subject toLoopersynchronization barriers.Certain operations, such as view invalidation, may introduce synchronization barriers into the
Looper's message queue to prevent subsequent messages from being delivered until some condition is met. In the case of view invalidation, messages which are posted after a call toandroid.view.View#invalidateare suspended by means of a synchronization barrier until the next frame is ready to be drawn. The synchronization barrier ensures that the invalidation request is completely handled before resuming.Asynchronous messages are exempt from synchronization barriers. They typically represent interrupts, input events, and other signals that must be handled independently even while other work has been suspended.
Note that asynchronous messages may be delivered out of order with respect to synchronous messages although they are always delivered in order among themselves. If the relative order of these messages matters then they probably should not be asynchronous in the first place. Use with caution.
- Parameters:
async- True if the message is asynchronous.- See Also:
isAsynchronous()
-
toString
public String toString()
Description copied from class:ObjectReturns 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
toStringmethod if you intend implementing your owntoStringmethod.
-
describeContents
public int describeContents()
Description copied from interface:ParcelableDescribe the kinds of special objects contained in this Parcelable's marshalled representation.- Specified by:
describeContentsin interfaceParcelable- Returns:
- a bitmask indicating the set of special object types marshalled by the Parcelable.
-
writeToParcel
public void writeToParcel(Parcel dest, int flags)
Description copied from interface:ParcelableFlatten this object in to a Parcel.- Specified by:
writeToParcelin interfaceParcelable- Parameters:
dest- The Parcel in which the object should be written.flags- Additional flags about how the object should be written. May be 0 orParcelable.PARCELABLE_WRITE_RETURN_VALUE.
-
-