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?

Kar4
  • 25
  • 8

1 Answers1

2

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
    }
  }
  • but there are too many users, taking all the info and getting the presence status of a person who I want would take much resources, I want to get presence status of a specified user – Kar4 Mar 02 '16 at 06:49
  • @Kar4 then try with your way by just adding this line Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all); roster.getPresence(name + "@" + HOST+ "/Smack") tell me if any issues. – Anhad Mathur Mar 02 '16 at 08:54
  • Oh, yes, it solved the problem, I'm sorry that I forgot to thank you) Thank you very much – Kar4 Apr 20 '16 at 05:52