2014-10-31 33 views
0

我用这个XMPP aSmack - How can I get the current user state (offline/online/away/etc.)?
代码与ejabbered和登录连接是安卓与Asmack - 如何检查用户在XMPP在android中的状态?

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 userFromServer=connection.getRoster().getPresence(connection.getUser()); 

我在做什么错所以我如何获得其他用户存在?

回答

1

您可以通过使用特定的用户

Presence availability = roster.getPresence(user); 

这里的用户ID获取用户的可用性:用户是指用户ID

+0

存在userFromServer = connection.getRoster()getPresence(connection.getUser。 ());当我使用这个我看到的值connection.getUser()= 151083569134563 @ acqut/Smack – Raj 2014-10-31 09:48:51

+0

我必须通过获取其他用户存在状态 – Raj 2014-10-31 09:49:25

+0

请参阅。首先你必须得到这个特定用户可用的所有用户名和ID ..然后传递该ID来获得该用户的存在。 – 2014-10-31 09:52:01