Roser#getPresence(String user) will return null if the contact has no presence information available. This could be because the contact is offline or you are not subscribed to their presence
If you are getting null when your contact is online then it will be (most likely) one of two things.
1: Either you are not subscribed to the other contacts Presence. In which case you will need to before you are allowed to track their Online/Offline status.
2: As AWenckus mentioned, it can sometimes take a moment for the information to be sent so if you are performing this operation as soon as you login the method may well return null.
Far better to add a RosterListener to your connection to listen out for Presence changes.
Presence presence = (Presence)packet;
Presence.Mode presenceMode = presence.getMode();
if(presenceMode == Presence.Mode.AVAILABLE) {
// user is online and available
}
if(presenceMode == Presence.Mode.AWAY) {
//user is away
}
take for example two users:User A,User B
User A wants to see when User B is online.
User A sends a SUBSCRIBE packet to User B
User B then decides whether or not to allow User A to see when they are online.
If they accept the subscription requests they send a SUBSCRIBED packet
If they reject the subscription request they send a UNSUBSCRIBED packet.
参考:http://www.igniterealtime.org/community/message/115077#115077
本文探讨了XMPP协议中在线状态的订阅机制。当一个用户想要跟踪另一个用户的在线状态时,需要先发送订阅请求。如果请求被接受,则可以接收其在线状态更新;若拒绝,则无法获取状态更新。此外,文章还介绍了如何通过监听Presence包来实时获取联系人的在线状态。
3096

被折叠的 条评论
为什么被折叠?



