我使用这个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 userFromServer=connection.getRoster().getPresence(connection.getUser());我做错了什么,那么我如何才能让其他用户在场呢?
发布于 2014-10-31 09:39:35
您可以通过使用特定用户的用户id来获得用户的可用性。
Presence availability = roster.getPresence(user);这里:用户意味着用户id。
https://stackoverflow.com/questions/26670762
复制相似问题