Android与Asmack - 如何在Android中检查XMPP中的用户状态?

时间:2014-10-31 08:58:55

标签: android xmpp asmack

我使用此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());

我做错了什么,以便我如何让其他用户出现?

1 个答案:

答案 0 :(得分:1)

您可以使用特定用户的用户ID

来获取用户的可用性
Presence availability = roster.getPresence(user);

此处:用户表示用户ID