Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

api

smack

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.

like image 309
chikka.anddev Avatar asked Jan 12 '11 06:01

chikka.anddev


4 Answers

    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.

like image 75
Andrew V. Avatar answered Nov 15 '22 21:11

Andrew V.


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

like image 31
Jignesh Dhua Avatar answered Nov 15 '22 21:11

Jignesh Dhua


Presence presence = roster.getPresence("[email protected]");
if (presence.getType() == Presence.Type.AVAILABLE) {
   // Tom is online...
}

reference from this link

like image 42
Zoombie Avatar answered Nov 15 '22 21:11

Zoombie


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();
          }
      }
});
like image 1
Vipin Yadav Avatar answered Nov 15 '22 22:11

Vipin Yadav