一 Presence对象介绍
与IQ一样,Presence也是继承自XMPP的基类Packet信息包,Presence主要有两个用途:1)告诉服务器所有客户端当前所处的状态,2)发出添加/删除好友请求;每个Presence信息包都有一个类型属性Presence.Type,如下:
- available: 表示处于在线状态
- unavailable: 表示处于离线状态
- subscribe: 表示发出添加好友的申请
- unsubscribe: 表示发出删除好友的申请
- unsubscribed: 表示拒绝添加对方为好友
- error: 表示presence信息报中包含了一个错误消息。
除了类型信息外,Presence还包含其他一些可选的属性:
- Status: 用于表示用户状态的自定义文本,例如:外出吃饭
- Priority: 一个表示发送者资源优先级的非负数(我尚未搞明白什么是发送者资源)
- Mode: 表示五种状态之一:在线(默认)、聊天、离开、xa (extended away)、请勿打扰
二 用法示例
下面的示例表示向服务器发送一个邀请加入好友的Presence信息包的代码段:
- <SPAN style="FONT-SIZE: 12px">Presence subscription = new Presence(Presence.Type.subscribe);
- subscription.setTo(packet.getFrom());
- ConnectionUtils.getConnection().sendPacket(subscription);</SPAN>
Presence subscription = new Presence(Presence.Type.subscribe);
subscription.setTo(packet.getFrom());
ConnectionUtils.getConnection().sendPacket(subscription);
三 方法汇总
isAvailable
public boolean isAvailable()
-
如果用户在线,返回true,反之,返回false;另外,如果对方的presence信息包中如果包含添加好友的申请,也会返回false。fai方法等价于getType()==Presence.Type.available。需要注意,当用户状态为在线时,其Presence信息包中的Mode属性值还是有可能为以下几种模式:离开(
away
),extended away
或者请勿打扰(do not disturb
). -
返回值:在线状态的布尔值
isAway
public boolean isAway()
-
Returns true if the presence type is
available
and the presence mode isaway
,extended away
, ordo not disturb
. False will be returned when the type or mode is any other value, including when the presence type is unavailable (offline). This is a convenience method equivalent totype == Type.available && (mode == Mode.away || mode == Mode.xa || mode == Mode.dnd). -
-
Returns:
- true if the presence type is available and the presence mode is away, xa, or dnd.
getType
public Presence.Type getType()
- Returns the type of this presence packet.
-
-
Returns:
- the type of the presence packet.
setType
public void setType(Presence.Type type)
- Sets the type of the presence packet.
-
-
Parameters:
-
type
- the type of the presence packet.
-
getStatus
public String getStatus()
- Returns the status message of the presence update, ornull if there is not a status. The status is free-form text describing a user's presence (i.e., "gone to lunch").
-
-
Returns:
- the status message.
setStatus
public void setStatus(String status)
- Sets the status message of the presence update. The status is free-form text describing a user's presence (i.e., "gone to lunch").
-
-
Parameters:
-
status
- the status message.
-
getPriority
public int getPriority()
- Returns the priority of the presence, or Integer.MIN_VALUE if no priority has been set.
-
-
Returns:
- the priority.
setPriority
public void setPriority(int priority)
- Sets the priority of the presence. The valid range is -128 through 128.
-
-
Parameters:
-
priority
- the priority of the presence.
Throws:
-
IllegalArgumentException
- if the priority is outside the valid range.
-
getMode
public Presence.Mode getMode()
-
Returns the mode of the presence update, or null if the mode is not set. A null presence mode value is interpreted to be the same thing as
Presence.Mode.available
. -
-
Returns:
- the mode.
setMode
public void setMode(Presence.Mode mode)
-
Sets the mode of the presence update. A null presence mode value is interpreted to be the same thing as
Presence.Mode.available
. -
-
Parameters:
-
mode
- the mode.
-
setLanguage
public void setLanguage(String language)
- Sets the xml:lang of this Presence.
-
-
Parameters:
-
language
- the xml:lang of this Presence.
Since:
-
toXML
public String toXML()
-
Description copied from class:
Packet
- Returns the packet as XML. Every concrete extension of Packet must implement this method. In addition to writing out packet-specific data, every sub-class should also write out the error and the extensions data if they are defined.
-
-
Returns:
- the XML format of the packet as a String.
toString
public String toString()