【问题标题】:Android with Asmack -How to check user presence status in XMPP in android?Android with Asmack - 如何在 android 的 XMPP 中检查用户存在状态?
【发布时间】:2014-12-27 13:01:34
【问题描述】:

我用这个XMPP aSmack - How can I get the current user state (offline/online/away/etc.)?
用于连接 ejabbered 和登录的代码是

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

我总是让 Presence 不可用,但如果我使用下面的代码,它可以正常工作

Presence userFromServer=connection.getRoster().getPresence(connection.getUser());

我做错了什么,我怎样才能让其他用户在场?

【问题讨论】:

    标签: android xmpp asmack


    【解决方案1】:

    您可以通过使用特定用户的用户ID来获取用户的可用性

    Presence availability = roster.getPresence(user);
    

    这里:用户是指用户ID

    【讨论】:

    • 在线状态 userFromServer=connection.getRoster().getPresence(connection.getUser());当我使用它时,我看到 connection.getUser()=151083569134563@acqut/Smack 中的值
    • 我必须通过什么才能获得其他用户的存在状态
    • 见。首先,您必须获取该特定用户中可用的所有用户名和 ID。然后传递该 ID 以获取该用户的存在。
    • 你的意思是说我无法通过在上面的代码中直接给出用户的 jid 来找到用户的存在状态
    • 您可以在 Presence 呼叫的用户 id 部分中获得该 JID 用户的存在。我的意思是说,如果您想获得与该登录用户相关的所有用户的存在(例如,如果您登录 fb,您将不知道我们的哪些朋友在线/离线)。
    猜你喜欢
    • 1970-01-01
    • 2014-05-02
    • 2011-12-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    相关资源
    最近更新 更多