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

送分小仙女□ 提交于 2019-12-04 16:19:11

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!