Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMPP aSmack - How can I get the current user state (offline/online/away/etc.)?

I am new to xmpp/asmack in android.

Can anyone please help me in getting the presence of the user's friends ( roster list)

I am using this :

Presence availability = roster.getPresence(user);
Mode userMode = availability.getMode();

What else should I do to get the availability status of each user listed in my roster.

like image 408
samsad Avatar asked Jan 22 '13 06:01

samsad


3 Answers

Just use like this :

Presence availability = roster.getPresence(user);
Mode userMode = availability.getMode();

retrieveState_mode(availability.getMode(),availability.isAvailable());

public static int retrieveState_mode(Mode userMode, boolean isOnline) {
        int userState = 0;
        /** 0 for offline, 1 for online, 2 for away,3 for busy*/
        if(userMode == Mode.dnd) {
            userState = 3;
        } else if (userMode == Mode.away || userMode == Mode.xa) {   
            userState = 2;
        } else if (isOnline) {
            userState = 1;
        }
        return userState;
}

Let me know if you have any problem regarding xmpp/asmack

like image 81
Gaurav Arora Avatar answered Nov 08 '22 08:11

Gaurav Arora


use like this

userFromServer = con.getRoster().getPresence(userID);
userState = retrieveState(userFromServer.getMode(), userFromServer.isAvailable());

public int retrieveState(Mode userMode, boolean isOnline) {
    int userState = XmppFriend.OFFLINE; // default return value
    if (userMode == Mode.dnd) {
        userState = XmppFriend.BUSY;
    } else if (userMode == Mode.away || userMode == Mode.xa) {
        userState = XmppFriend.AWAY;
    } else if (isOnline) {
        userState = XmppFriend.ONLINE;
    }
    return userState;
}
like image 27
NagarjunaReddy Avatar answered Nov 08 '22 09:11

NagarjunaReddy


        roster.addRosterListener(new RosterListener() {

        public void entriesAdded(Collection<String> param) {}

        public void entriesDeleted(Collection<String> addresses) {
        }
        public void entriesUpdated(Collection<String> addresses) {  
        }
        public void presenceChanged(Presence presence) {

            String user = presence.getFrom();
            Presence bestPresence = roster.getPresence(user);

            Log.d(TAG, "BestPresence: " + user + ": " + bestPresence);

            String[] temp = presence.getFrom().split("\\@");
            Log.d(TAG, "Presence: " + temp[0] + "-" + presence.toString());

            String status = presence.toString();
            // ShowInfoDialog(temp[0]+"is "+status);

            for (int i = 0; i < friendslist.size(); i++) {

                if (temp[0].equalsIgnoreCase(friendslist.get(i).getName())) {

                    friendslist.get(i).setStatus(status);
                    Log.d(TAG, "kilepet/belepet " + friendslist.get(i).getName() + " - " + friendslist.get(i).getStatus());

                    // ShowInfoDialog(friendslist.get(i).getName()+"is "+status);
                    Log.d(TAG, "WATERFAK");

                }
            }

        }

If you use RosterListener, it updates the presence in real time, it works for me just fine.

like image 1
cesztoszule Avatar answered Nov 08 '22 09:11

cesztoszule