Android class reference-message

Source: Internet
Author: User

1. inheritance relationship

Java. Lang. Object

| _____ Android. OS. Message

Implement the parcelable interface.

2. class overview

This class defines a message that can be sent to the handler object. It contains the Message description and any data object. This object contains two int fields and one object field. In many cases, you do not need to allocate data to these fields.

Although the message class constructor is public, the best way to obtain its instance is to call message. obtain () method or handler. the obtainmessage () method, which obtains an obsolete message object from the recycle Object pool.

3. Public Field

Public static final creator <message> creator

Public int arg1: If you only need to keep several integers, replacing the setdata () method with arg1 and arg2 will reduce the system overhead.

Public int arg2: If you only need to keep several integers, replacing the setdata () method with arg1 and arg2 will reduce the system overhead.

Public object OBJ: any type of object to be sent to the receiver. When the messageer object is used to send messages across processes, if it contains a parcelable Implementation of the Framework class (not implemented by the application), the object can only be non-empty. Setdata (bundle) is used for transmission of other data ).

Public messenger replyto: sets the Messager object that can receive the response from this message. The exact semantics should use the sender and receiver.

Public int what: User-defined message encoding, which allows the receiver to express the message. Each handler object has its own message encoding namespace, so there is no need to worry about conflicts with other handler.

4. Public Constructor

Public message ()

Constructor, but the best way to get the message object is to call the message. Obtain () method.

5. Public Method

Public void copyfrom (Message O)

Copy the image of the data field of message object o. It cannot copy the linked list field, or copy the time stamp, target, and callback method of the original message.

Public int describecontents ()

This method describes the types of special objects in the parcelable group.

Returned value: A bitmask that indicates a set of special object types grouped by parcelable objects.

Public runnable getcallback ()

Gets the callback object to be executed when a message is processed. This object must implement the runnable interface. This object called by the target handler object will receive the message object distributed to it. If no value is set, the message is distributed to the handlemessage (Message) method that accepts the handler object.

Public bundle getdata ()

Obtain the bundle object of any data associated with this event. If needed, it will be created later. Call the setdata (bundle) method to set the value to be passed. It should be noted that when data is transferred across processes through the Messager object, the bundle must be used. setclassloader () method to set the bundle-related classloader so that it can be instantiated as a specific object when obtaining data.

Public handler gettarget ()

Gets the implementation of the handler object that receives messages. This object must implement the handler. handlemessage () method. Each handler object has its own message encoding namespace, so there is no need to worry about conflicts with other handler objects.

Public long getwhen ()

In milliseconds, the time when the message is sent to the target is returned.

Public static message obtain (handler H, int what, int arg1, int arg2, object OBJ)

The method is the same as the obtain () method, but the values of target, what, arg1, arg2, and OBJ member variables must be set.

Parameter description:

H: The target handler object for processing the message;

What: message encoding;

Arg1: Additional integer data;

Arg2: Additional integer data;

OBJ: object type data of the attachment.

Return Value: return a message object from the Global Object pool.

Public static message obtain (handler H, int what, object OBJ)

The method is the same as the obtain () method, but the value of the target, what, and OBJ member variables must be set.

Parameter description:

H: The target handler object for processing the message;

What: message encoding;

OBJ: object type data of the attachment.

Return Value: return a message object from the Global Object pool.

Public static message obtain (handler H, int what)

The method is the same as the obtain method, but the target and what member variables related to the message object must be set.

Parameter description:

H: The target handler object for processing the message;

What: message encoding;

Return Value: return a message object from the Global Object pool.

Public static message obtain (handler H)

The method is the same as the obtain method, but the target and what member variables related to the message object must be set.

Parameter description:

H: The target handler object for processing the message;

Return Value: return a message object from the Global Object pool.

Public static message obtain (handler H, runnable callback)

The method is the same as the obtain method, but a runnable object must be assigned to the returned message object.

Parameter description:

H: The target handler object for processing the message;

Callback: the runnable object to be executed when processing a message.

Return Value: return a message object from the Global Object pool.

Public static message obtain ()

Return a new message instance from the Global Object pool. In many cases, this avoids system overhead for creating new objects.

Public static message obtain (handler H, int what, int arg1, int arg2)

It is the same as the obtain () method, but the value of the target, what, arg1, and arg2 member variables must be set.

Parameter description:

H: The target handler object for processing the message;

What: message encoding;

Arg1: Additional integer data;

Arg2: Additional integer data;

Return Value: return a message object from the Global Object pool.

Public static message obtain (Message orig)

The method is the same as the obtain () method, but it copies a existing message object (including its target) to a new message object.

Parameter description:

Orig: the original message to be copied.

Return Value: return a message object from the Global Object pool.

Public bundle peekdata ()

It is a bit like getdata (), but it does not delay creating the bundle object. If the bundle object does not exist, null is returned.

Public void recycle ()

Returns an instance of a message object to the Global Object pool. After this method is called, you do not need to pay attention to this message object, and it will be effectively released.

Public void sendtotarget ()

Send the message object to the handler object specified by the gettarget () method. If this member variable is not set, a null pointer exception is thrown.

Public void setdata (bundle data)

Set a bundler object of any data type for the message object. If possible, use the arg1 and arg2 member variables to send simple integers, which can effectively reduce system overhead.

Public void settarget (handler target)

Set the handler object for processing the message.

Public sting tostring ()

Return a concise description of the object that the layman can understand. The subclass is encouraged to override this method and provide the types and data of the objects to be considered for implementation. The default implementation is as follows:

Getclass (). getname ()
+ '@'
+ Integer. tohexstring (hashcode ())

Return Value: return the description of a printable object.

Public void writetoparcel (parcel DEST, int flags)

Merge the message object into a parcel object.

Parameter description:

DeST: Specifies the parcel type object to be written.

Flags: specify how the message object should be written to the additional tag of the parcel object. It can be 0 or parcelable_write_return_value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.