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

江枫思渺然 提交于 2020-01-01 18:57:52

问题


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 ..?


回答1:


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.



来源:https://stackoverflow.com/questions/20991060/how-to-get-list-of-offline-online-users-in-chat-by-using-smack-api-in-android

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