How to get list of offline/online users in chat by using smack api in android

meera picture meera · Jan 8, 2014 · Viewed 7.4k times · Source

I am writing a chat application in android by using xmpp and smack api. Chat is working successfully when i am entering email id of particular friend for sending chat.but i am not able to get list of offline/online users.Please suggest how to get list of users using xmpp smack ..?

Answer

AnAndroid picture AnAndroid · Jan 8, 2014

You have to make a listView to get list of users and try this code

 public static  ArrayList<HashMap<String, String>> usersList=new ArrayList<HashMap<String, String>>();


            Presence presence = new Presence(Presence.Type.available);
            Constants.connection.sendPacket(presence);
            setConnection(Constants.connection);

            final Roster roster = Constants.connection.getRoster();
            Collection<RosterEntry> entries = roster.getEntries();

            for (RosterEntry entry : entries) {

                    HashMap<String, String> map = new HashMap<String, String>();
                    Presence entryPresence = roster.getPresence(entry.getUser());

                    Presence.Type type = entryPresence.getType();       

                    map.put("USER", entry.getName().toString());
                    map.put("STATUS", type.toString());
                    Log.e("USER", entry.getName().toString());

                    usersList.add(map);

            }

And then add your userList to your ListAdapter and check STATUS is equals to 'avialable' then the user is online otherwise user is Offline.