javaandroidxmppuser-presencerostering

XMPP roster getPresence status of a user (java)


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?


Solution

  • 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
        }
      }