我可以通过 Smack 将在线用户添加到我的好友列表中吗?

发布于 2024-10-11 10:04:34 字数 119 浏览 6 评论 0原文

我可以通过 Smack API 将在线用户添加到我的好友列表中吗?是否可以?

我正在开发可以在用户之间聊天的应用程序。我已经成功创建了聊天应用程序示例,只需输入朋友的姓名并发送聊天,但现在我想要在线朋友列表。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

没︽人懂的悲伤 2024-10-18 10:04:34
Roster roster = xmppConnection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
Presence presence;

for(RosterEntry entry : entries) {
    presence = roster.getPresence(entry.getUser());

    System.out.println(entry.getUser());
    System.out.println(presence.getType().name());
    System.out.println(presence.getStatus());
}
Roster roster = xmppConnection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
Presence presence;

for(RosterEntry entry : entries) {
    presence = roster.getPresence(entry.getUser());

    System.out.println(entry.getUser());
    System.out.println(presence.getType().name());
    System.out.println(presence.getStatus());
}
相守太难 2024-10-18 10:04:34
    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());
            }

        }
    });

因此,在程序开始时注册 XMPPConnection 侦听器,通常需要几秒钟才能接收连接对象。
但只有当您使用 creatEntry 时,它才会起作用,只有在这种情况下,rooster 才会看到那些创建的用户。

要使用名册创建条目,请使用下一个代码:

try {
    rooster.createEntry("name", "user_id", null);
} catch (XMPPException e) {
    e.printStackTrace();
}

我没有使用任何组,并且成功地在第二台设备上看到用户。

    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.

难理解 2024-10-18 10:04:34
Presence presence = roster.getPresence("[email protected]");
if (presence.getType() == Presence.Type.AVAILABLE) {
   // Tom is online...
}

参考此链接

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

reference from this link

徒留西风 2024-10-18 10:04:34

使用 presence.getMode() 方法获取用户的模式。
Mode 是枚举,其值可以是 chat、available、a​​way、xa、dnd。

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

旧话新听 2024-10-18 10:04:34
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();
          }
      }
});
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();
          }
      }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文