我使用这个XMPP aSmack-我如何获得当前用户状态(离线/在线/离开等)
用于连接ejabbred并登录的代码是
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());
我做错了什么,我该如何获得其他用户的存在感?
您可以通过使用特定用户的用户id来获得用户的可用性
Presence availability = roster.getPresence(user);
这里:user表示用户id