English | Site Directory

Android - An Open Handset Alliance Project

android.os
public final class

android.os.Message

java.lang.Object
android.os.Message Parcelable

Defines a Message that can be placed in a MessageQueue for scheduled execution of a Runnable object. The Message must be given a Handler that is in charge scheduling the Message and managing its results.

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

Summary

Constants

      Value  
Creator  CREATOR       

Fields

public      int  arg1  arg1 and arg2 are lower-cost alternatives to using setData() if you only need to store a few integer values. 
public      int  arg2  arg1 and arg2 are lower-cost alternatives to using setData() if you only need to store a few integer values. 
public      Runnable  callback  A callback object to receive the message. 
public      Object  obj  An arbitrary object to send to the recipient. 
public      Handler  target  A Handler implementation that will receive this message. 
public      int  what  User-defined message code so that the recipient can identify what this message is about. 
public      long  when  The targeted delivery time of this message,in milliseconds from now. 

Public Constructors

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

Public Methods

        void  copyFrom(Message o)
Make this message like o.
        HashMap  getData()
Obtains a HashMap of arbitrary data associated with this event, lazily creating it if necessary.
      static  Message  obtain()
Return a new Message instance from the global pool.
      static  Message  obtain(Handler h)
Same as obtain(), but sets the value for the target member on the Message returned.
      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.
      static  Message  obtain(Handler h, int what)
Same as obtain(), but sets the values for both target and what members on the Message.
      static  Message  obtain(Handler h, int what, Object obj)
Same as obtain(), but sets the values of the target, what, and obj members.
      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.
        HashMap  peekData()
Like getData(), but does not lazily create the HashMap.
        void  recycle()
Return a Message instance to the global pool.
        void  sendToTarget()
Sends this Message to the Handler specified by target.
        void  setData(HashMap data)
Sets a HashMap of arbitrary data values.
        String  toString()
Answers a string containing a concise, human-readable description of the receiver.
        void  writeToParcel(Parcel dest)
Writes all of the Message's data to a given Parcel object, including any HashMap data.
Methods inherited from class java.lang.Object
Methods inherited from interface android.os.Parcelable

Details

Constants

public static final Creator CREATOR

Fields

public int arg1

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

public int arg2

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

public Runnable callback

A callback object to receive the message. 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).

public Object obj

An arbitrary object to send to the recipient.

public Handler target

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.

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.

public long when

The targeted delivery time of this message,in milliseconds from now.

Public Constructors

public Message()

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

Public Methods

public void copyFrom(Message o)

Make this message like o. Performs a shallow copy of the data field. Does not copy the HashMap data.

Parameters

o The Message to copy over to this one.

public HashMap getData()

Obtains a HashMap of arbitrary data associated with this event, lazily creating it if necessary. Set this value by calling setData(HashMap).

public static Message obtain()

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

public static Message obtain(Handler h)

Same as obtain(), 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.

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

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.

public static Message obtain(Handler h, int what)

Same as obtain(), 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.

public static Message obtain(Handler h, int what, Object obj)

Same as obtain(), 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.

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

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.

public HashMap peekData()

Like getData(), but does not lazily create the HashMap. A null is returned if the HashMap does not already exist. Set this value by calling setData(HashMap).

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

public void sendToTarget()

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

public void setData(HashMap data)

Sets a HashMap of arbitrary data values. Use arg1 and arg1 members as a lower cost way to send a few simple integer values, if you can. Retrieve the data with getData() or peekData().

public String toString()

Answers a string containing a concise, human-readable description of the receiver.

public void writeToParcel(Parcel dest)

Writes all of the Message's data to a given Parcel object, including any HashMap data.

Parameters

dest The destination Parcel for the data.
Build m5-rc15g - 14 May 2008 12:50