Most visited

Recently visited

Added in API level 1

Messenger

public final class Messenger
extends Object implements Parcelable

java.lang.Object
   ↳ android.os.Messenger


引用一个Handler,其他人可以用它来发送消息给它。 这允许跨进程实现基于消息的通信,方法是在一个进程中创建指向Handler的Messenger,然后将该Messenger移交给另一个进程。

注意:下面的实现只是用于执行通信的Binder的简单包装。 这意味着你应该像这样对待它:这个类不影响流程生命周期管理(你必须使用一些更高级别的组件来告诉系统你的流程需要继续运行),如果你的流程消失,连接将会中断出于任何原因等等

Summary

Inherited constants

From interface android.os.Parcelable

Fields

public static final Creator<Messenger> CREATOR

Public constructors

Messenger(Handler target)

创建一个指向给定Handler的新Messenger。

Messenger(IBinder target)

从一个原始的IBinder创建一个Messenger,以前用 getBinder()进行检索。

Public methods

int describeContents()

描述此Parcelable实例的封送表示中包含的特殊对象的种类。

boolean equals(Object otherObj)

两个Messenger对象的比较运算符,例如返回true,然后它们都指向同一个Handler。

IBinder getBinder()

检索此Messenger正在使用的IBinder与其关联的处理程序进行通信。

int hashCode()

返回对象的哈希码值。

static Messenger readMessengerOrNullFromParcel(Parcel in)

便捷功能,用于从Parcel中读取Messenger或空指针。

void send(Message message)

发送消息给这个Messenger的处理程序。

static void writeMessengerOrNullToParcel(Messenger messenger, Parcel out)

便捷功能,用于向信包写入信使或空指针。

void writeToParcel(Parcel out, int flags)

将此对象平铺到一个包裹中。

Inherited methods

From class java.lang.Object
From interface android.os.Parcelable

Fields

CREATOR

Added in API level 1
Creator<Messenger> CREATOR

Public constructors

Messenger

Added in API level 1
Messenger (Handler target)

创建一个指向给定Handler的新Messenger。 通过这个Messenger发送的任何消息对象都将出现在处理程序中,就像Handler.sendMessage(Message)已被直接调用一样。

Parameters
target Handler: The Handler that will receive sent messages.

Messenger

Added in API level 1
Messenger (IBinder target)

从原始IBinder创建一个Messenger,以前使用 getBinder()进行检索。

Parameters
target IBinder: The IBinder this Messenger should communicate with.

Public methods

describeContents

Added in API level 1
int describeContents ()

描述此Parcelable实例的封送表示中包含的特殊对象的种类。 例如,如果对象将在writeToParcel(Parcel, int)的输出中包含writeToParcel(Parcel, int) ,则此方法的返回值必须包含CONTENTS_FILE_DESCRIPTOR位。

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance.

equals

Added in API level 1
boolean equals (Object otherObj)

两个Messenger对象的比较运算符,例如返回true,然后它们都指向同一个Handler。

Parameters
otherObj Object: the reference object with which to compare.
Returns
boolean true if this object is the same as the obj argument; false otherwise.

getBinder

Added in API level 1
IBinder getBinder ()

检索此Messenger正在使用的IBinder与其关联的处理程序进行通信。

Returns
IBinder Returns the IBinder backing this Messenger.

hashCode

Added in API level 1
int hashCode ()

返回对象的哈希码值。 这种方法支持散列表的好处,例如HashMap提供的HashMap

hashCode的总合同是:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

尽可能合理实用,由类Object定义的hashCode方法确实为不同的对象返回不同的整数。 (这通常通过将对象的内部地址转换为整数来实现,但Java TM编程语言不需要此实现技术。)

Returns
int a hash code value for this object.

readMessengerOrNullFromParcel

Added in API level 1
Messenger readMessengerOrNullFromParcel (Parcel in)

便捷功能,用于从Parcel中读取Messenger或空指针。 你必须先写信使用writeMessengerOrNullToParcel(Messenger, Parcel)

Parameters
in Parcel: The Parcel containing the written Messenger.
Returns
Messenger Returns the Messenger read from the Parcel, or null if null had been written.

send

Added in API level 1
void send (Message message)

发送消息给这个Messenger的处理程序。

Parameters
message Message: The Message to send. Usually retrieved through Message.obtain().
Throws
RemoteException Throws DeadObjectException if the target Handler no longer exists.

writeMessengerOrNullToParcel

Added in API level 1
void writeMessengerOrNullToParcel (Messenger messenger, 
                Parcel out)

便捷功能,用于向信包写入信使或空指针。 您必须将其与readMessengerOrNullFromParcel(Parcel)一起使用, readMessengerOrNullFromParcel(Parcel)供以后阅读。

Parameters
messenger Messenger: The Messenger to write, or null.
out Parcel: Where to write the Messenger.

writeToParcel

Added in API level 1
void writeToParcel (Parcel out, 
                int flags)

将此对象平铺到一个包裹中。

Parameters
out Parcel: The Parcel in which the object should be written.
flags int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.

Hooray!