smack presence问题

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

你可能感兴趣的:(smack,presence)