一 Roster對象介紹
将其翻譯成中文為"花名冊",用來表示一個使用者的所有好友清單以及申請加好友的使用者清單,為了便于管理,Roster中的使用者分組進行管理。當其他使用者想申請加好友時,需要向該使用者發出一個subcription請求,處理請求的模式有三種:
accept_all: 接收所有請求;
reject_all: 拒絕所有請求;
manual: 手工處理所有請求
二 常用方法
getDefaultSubscriptionMode
public static Roster.SubscriptionModegetDefaultSubscriptionMode()
- Returns the default subscription processing mode to use when a new Roster is created. The subscription processing mode dictates what action Smack will take when subscription requests from other users are made. The default subscription mode is
.Roster.SubscriptionMode.accept_all
-
- Returns:
- the default subscription mode to use for new Rosters
setDefaultSubscriptionMode
public static void setDefaultSubscriptionMode(Roster.SubscriptionMode subscriptionMode)
- Sets the default subscription processing mode to use when a new Roster is created. The subscription processing mode dictates what action Smack will take when subscription requests from other users are made. The default subscription mode is
.Roster.SubscriptionMode.accept_all
-
- Parameters:
-
- the default subscription mode to use for new Rosters.subscriptionMode
-
getSubscriptionMode
public Roster.SubscriptionMode getSubscriptionMode()
- Returns the subscription processing mode, which dictates what action Smack will take when subscription requests from other users are made. The default subscription mode is
Roster.SubscriptionMode.accept_all
.
If using the manual mode, a PacketListener should be registered that listens for Presence packets that have a type of
.Presence.Type.subscribe
-
- Returns:
- the subscription mode.
setSubscriptionMode
public void setSubscriptionMode(Roster.SubscriptionMode subscriptionMode)
- Sets the subscription processing mode, which dictates what action Smack will take when subscription requests from other users are made. The default subscription mode is
Roster.SubscriptionMode.accept_all
.
If using the manual mode, a PacketListener should be registered that listens for Presence packets that have a type of
.Presence.Type.subscribe
-
- Parameters:
-
- the subscription mode.subscriptionMode
-
reload
public void reload()
- Reloads the entire roster from the server. This is an asynchronous operation, which means the method will return immediately, and the roster will be reloaded at a later point when the server responds to the reload request.
-
- Throws:
-
- if connection is not logged in or logged in anonymouslyIllegalStateException
-
addRosterListener
public void addRosterListener(RosterListener rosterListener)
- Adds a listener to this roster. The listener will be fired anytime one or more changes to the roster are pushed from the server.
-
- Parameters:
-
- a roster listener.rosterListener
-
removeRosterListener
public void removeRosterListener(RosterListener rosterListener)
- Removes a listener from this roster. The listener will be fired anytime one or more changes to the roster are pushed from the server.
-
- Parameters:
-
- a roster listener.rosterListener
-
createGroup
public RosterGroup createGroup(String name)
-
Creates a new group.
Note: you must add at least one entry to the group for the group to be kept after a logout/login. This is due to the way that XMPP stores group information.
-
- Parameters:
-
- the name of the group.name
Returns: - a new group. Throws:
-
- if connection is not logged in or logged in anonymouslyIllegalStateException
-
createEntry
public void createEntry(String user,
String name,
String[] groups)
throws XMPPException
- Creates a new roster entry and presence subscription. The server will asynchronously update the roster with the subscription status.
-
- Parameters:
-
- the user. (e.g. [email protected])user
-
- the nickname of the user.name
-
- the list of group names the entry will belong to, ornull if the the roster entry won't belong to a group.groups
Throws: -
- if an XMPP exception occurs.XMPPException
-
- if connection is not logged in or logged in anonymouslyIllegalStateException
-
removeEntry
public void removeEntry(RosterEntry entry)
throws XMPPException
- Removes a roster entry from the roster. The roster entry will also be removed from the unfiled entries or from any roster group where it could belong and will no longer be part of the roster. Note that this is an asynchronous call -- Smack must wait for the server to send an updated subscription status.
-
- Parameters:
-
- a roster entry.entry
Throws: -
- if an XMPP error occurs.XMPPException
-
- if connection is not logged in or logged in anonymouslyIllegalStateException
-
getEntryCount
public int getEntryCount()
- Returns a count of the entries in the roster.
-
- Returns:
- the number of entries in the roster.
getEntries
public Collection<RosterEntry> getEntries()
- Returns an unmodifiable collection of all entries in the roster, including entries that don't belong to any groups.
-
- Returns:
- all entries in the roster.
getUnfiledEntryCount
public int getUnfiledEntryCount()
- Returns a count of the unfiled entries in the roster. An unfiled entry is an entry that doesn't belong to any groups.
-
- Returns:
- the number of unfiled entries in the roster.
getUnfiledEntries
public Collection<RosterEntry> getUnfiledEntries()
- Returns an unmodifiable collection for the unfiled roster entries. An unfiled entry is an entry that doesn't belong to any groups.
-
- Returns:
- the unfiled roster entries.
getEntry
public RosterEntry getEntry(String user)
- Returns the roster entry associated with the given XMPP address ornull if the user is not an entry in the roster.
-
- Parameters:
-
- the XMPP address of the user (eg "[email protected]"). The address could be in any valid format (e.g. "domain/resource", "[email protected]" or "[email protected]/resource").user
Returns: - the roster entry or null if it does not exist.
-
contains
public boolean contains(String user)
- Returns true if the specified XMPP address is an entry in the roster.
-
- Parameters:
-
- the XMPP address of the user (eg "[email protected]"). The address could be in any valid format (e.g. "domain/resource", "[email protected]" or "[email protected]/resource").user
Returns: - true if the XMPP address is an entry in the roster.
-
getGroup
public RosterGroup getGroup(String name)
- Returns the roster group with the specified name, ornull if the group doesn't exist.
-
- Parameters:
-
- the name of the group.name
Returns: - the roster group with the specified name.
-
getGroupCount
public int getGroupCount()
- Returns the number of the groups in the roster.
-
- Returns:
- the number of groups in the roster.
getGroups
public Collection<RosterGroup> getGroups()
- Returns an unmodifiable collections of all the roster groups.
-
- Returns:
- an iterator for all roster groups.
getPresence
public Presence getPresence(String user)
-
Returns the presence info for a particular user. If the user is offline, or if no presence data is available (such as when you are not subscribed to the user's presence updates), unavailable presence will be returned.
If the user has several presences (one for each resource), then the presence with highest priority will be returned. If multiple presences have the same priority, the one with the "most available" presence mode will be returned. In order, that's
,free to chat
,available
,away
, andextended away
do not disturb
.
Note that presence information is received asynchronously. So, just after logging in to the server, presence values for users in the roster may be unavailable even if they are actually online. In other words, the value returned by this method should only be treated as a snapshot in time, and may not accurately reflect other user's presence instant by instant. If you need to track presence over time, such as when showing a visual representation of the roster, consider using a
.RosterListener
-
- Parameters:
-
- an XMPP ID. The address could be in any valid format (e.g. "domain/resource", "[email protected]" or "[email protected]/resource"). Any resource information that's part of the ID will be discarded.user
Returns: - the user's current presence, or unavailable presence if the user is offline or if no presence information is available..
-
getPresenceResource
public Presence getPresenceResource(String userWithResource)
- Returns the presence info for a particular user's resource, or unavailable presence if the user is offline or if no presence information is available, such as when you are not subscribed to the user's presence updates.
-
- Parameters:
-
- a fully qualified XMPP ID including a resource ([email protected]/resource).userWithResource
Returns: - the user's current presence, or unavailable presence if the user is offline or if no presence information is available.
-
getPresences
public Iterator<Presence> getPresences(String user)
- Returns an iterator (of Presence objects) for all of a user's current presences or an unavailable presence if the user is unavailable (offline) or if no presence information is available, such as when you are not subscribed to the user's presence updates.
-
- Parameters:
-
- a XMPP ID, e.g. [email protected].user
Returns: - an iterator (of Presence objects) for all the user's current presences, or an unavailable presence if the user is offline or if no presence information is available.
-
三 Roster對象的擷取
使用Connection.getRoster()方法來擷取一個使用者的Roster執行個體,需要注意的是:無論使用者是否登入、或是否匿名登入,該方法傳回的Roster對象都不會為空,不過在上述情況下,不能使用Roster對象的大部分方法,否則會抛出異常。