Can I get online users in my friend list via Smack?
Asked Answered
U

5

10

Can i get online users in my friend list via Smack API? Is it possible?

I am working on app which have chat between users. I had successfully created chat application example just entering name of friend and send chat, but now I want online friends list.

Uzzia answered 12/1, 2011 at 6:29 Comment(0)
C
21
Roster roster = xmppConnection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
Presence presence;

for(RosterEntry entry : entries) {
    presence = roster.getPresence(entry.getUser());

    System.out.println(entry.getUser());
    System.out.println(presence.getType().name());
    System.out.println(presence.getStatus());
}
Chorizo answered 12/1, 2011 at 6:39 Comment(4)
Thanks,butin which i can determine status online or offline can u clear this?Uzzia
I did the same code but every user simply gives presence "unavailable" how it can be when there are 4-5 already available, but showing me all are unavailable can anyone help me on that?Araceli
Same here. I just get the status unavailable :/Sirkin
'xmppConnection.getRoster()' is not giving the updated roster entries. Have any idea about that?Mcmann
S
6
    XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {

        @Override
        public void connectionCreated(Connection arg0) {
            Log.i(TAG, "receive xmpp connection : " + arg0);
            connection = arg0;
            roster = arg0.getRoster();

            Collection<RosterEntry> entries = roster.getEntries();
            Presence presence;

            Log.e(TAG, "user count" + entries.size());

            for (RosterEntry entry : entries) {
                presence = roster.getPresence(entry.getUser());

                Log.i(TAG, "" + entry.getUser());
                Log.i(TAG, "" + presence.getType().name());
                Log.i(TAG, "" + presence.getStatus());
            }

        }
    });

So at the start of your program register that XMPPConnection listener, usually it take few seconds to receive connection object. But it will work only if you will use creatEntry only in that case rooster will see those created users.

To creat entry using Roster use next code:

try {
    rooster.createEntry("name", "user_id", null);
} catch (XMPPException e) {
    e.printStackTrace();
}

I didn't use any group, and with success see user on second device.

Specialist answered 12/2, 2014 at 12:53 Comment(0)
F
4
Presence presence = roster.getPresence("[email protected]");
if (presence.getType() == Presence.Type.AVAILABLE) {
   // Tom is online...
}

reference from this link

Friesland answered 9/2, 2012 at 13:25 Comment(1)
i need to ask few question regarding xmpp, i am able to get my roster friends in a list view now i want to show only online users in a listview what is the way will u please guide meReseta
C
1

Use the presence.getMode() method to get Mode of User. Mode is enum and its value can be chat, available, away, xa, dnd.

Chorizo answered 12/1, 2011 at 12:13 Comment(0)
F
1
smackAndroid = SmackAndroid.init(this);
XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {

    @Override
    public void connectionCreated(XMPPConnection connection) {

          Log.i("hello", "receive xmpp connection : " + connection);
          roster = connection.getRoster();

          try {
              roster.createEntry("2868254", "hello", null);
          } catch (XMPPException e) {
              e.printStackTrace();
          } catch (NotLoggedInException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (NoResponseException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (NotConnectedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
});
Faeroese answered 25/4, 2015 at 13:31 Comment(1)
Can you add some explanation to this?Fishplate

© 2022 - 2024 — McMap. All rights reserved.