如何在android中使用smack api获取聊天中的离线/在线用户列表 [英] How to get list of offline/online users in chat by using smack api in android

查看:93
本文介绍了如何在android中使用smack api获取聊天中的离线/在线用户列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xmpp和smack api在android中编写聊天应用程序。当我输入特定朋友的电子邮件ID发送聊天时聊天工作成功。但是我无法获得离线/在线用户列表。请建议如何使用xmpp smack获取用户列表..?

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

推荐答案

你必须制作一个listView以获取用户列表并试用此代码

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);

            }

然后将您的userList添加到ListAdapter并检查STATUS是等于'avialable'然后用户在线,否则用户处于离线状态。

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

这篇关于如何在android中使用smack api获取聊天中的离线/在线用户列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆