1

I'm using this code to get the presence status of a user

   Roster roster = connection.getRoster();
   Presence userPresence = roster.getPresence(name + "@" + HOST);

But userPresence always returns "unavailable" although the user is online. So what am I doing wrong, How can I get the presence status of a user?

4

1 回答 1

1

First try to get RosterEntries in a Collection using

Collection<RosterEntry> collection = roster.getEntries();

Then try to traverse each entry and check for presence

for (RosterEntry rosterEntry : collection)
  {
    Presence presence = null;
    presence = roster.getPresence(rosterEntry.getUser());

    if(presence.isAvailable())
    {
      //Do Something
    }
    else{
      //Do Something else
    }
  }
于 2016-03-02T06:43:09.950 回答