首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android与Asmack一起检查安卓系统中XMPP中的用户存在状态?

android与Asmack一起检查安卓系统中XMPP中的用户存在状态?
EN

Stack Overflow用户
提问于 2014-10-31 08:58:54
回答 1查看 2.9K关注 0票数 1

我使用这个XMPP aSmack - How can I get the current user state (offline/online/away/etc.)?

连接ejabbered和登录的代码是

代码语言:javascript
复制
Class.forName("org.jivesoftware.smack.ReconnectionManager");
System.setProperty("smack.debugEnabled", "true");
ConnectionConfiguration connConfig = new ConnectionConfiguration(
        XmppUtils.HOST, XmppUtils.PORT, XmppUtils.SERVICE);
connConfig.setSecurityMode(SecurityMode.disabled);
connConfig.setSendPresence(true);
XMPPConnection   connection = new XMPPConnection(connConfig);
SASLAuthentication.supportSASLMechanism("PLAIN");
connConfig.setSASLAuthenticationEnabled(true);
connection.connect();                   
connection.login(Fblogin.fb_id, XmppUtils.PASSWORD);
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
setConnection(connection);

//Check the user onlcine or offline
Presence userFromServer=connection.getRoster().getPresence(TOCHATUSERNAME+"/Smack");
final int userState = XmppUtils.retrieveState(userFromServer.getMode(), userFromServer.isAvailable());

// Set the state
getActivity().runOnUiThread(new Runnable() {
     @Override
     public void run() {
         if (userState==0) {
                txt_membersgname.setText("offline");
         } else if (userState==1) {
                txt_membersgname.setText("online");
         } else if (userState==2) {
                txt_membersgname.setText("Busy");
         }
    }
});


public static int retrieveState(Mode userMode, boolean isOnline) {
    /** 0 for offline, 1 for online, 2 for away,3 for busy*/
    int userState = 0; // default return value
    if (userMode == Mode.dnd) {
        Log.e("busy", "busy");
        userState = 3;
    } else if (userMode == Mode.away || userMode == Mode.xa) {
        userState =2;
    } else if (isOnline) {
        Log.e("online", "online");
        userState = 1;
    }
    return userState;
}

我总是显示不可用,但是如果我使用下面的代码,它是正确的。

代码语言:javascript
复制
Presence userFromServer=connection.getRoster().getPresence(connection.getUser());

我做错了什么,那么我如何才能让其他用户在场呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-31 09:39:35

您可以通过使用特定用户的用户id来获得用户的可用性。

代码语言:javascript
复制
Presence availability = roster.getPresence(user);

这里:用户意味着用户id。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26670762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档