I've been searching alot. but couldn't find anything. I need to refresh state of the contacts with rosterlistener. (offline/online). WHen creating roster i do:
Presence presence = roster.getPresence(r.getUser());
if(presence.isAvailable()){
userstatus.add("online");
}else{
userstatus.add("offline");
}
userstatus is a vector string, then i make a string array from it. In my main program i just check this stirng array for the offline or online. But what to do if user goes offline/online. WHere exactly i must put the listener and how exactly to use it?
You implement a RosterListener
and make use of the presenceChanged()
method. Here's sample code exactly as it appears in the Smack documentation .
Roster roster = con.getRoster();
roster.addRosterListener(new RosterListener() {
// Ignored events public void entriesAdded(Collection<String> addresses) {}
public void entriesDeleted(Collection<String> addresses) {}
public void entriesUpdated(Collection<String> addresses) {}
public void presenceChanged(Presence presence) {
System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
}
});
Note that the JavaDoc suggests not using the presence received in the change event but checking for the best presence available each time:
To get the current "best presence" for a user after the presence update, query the roster:
String user = presence.getFrom();
Presence bestPresence = roster.getPresence(user);
That will return the presence value for the user with the highest priority and availability.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.