1

I use this XMPP aSmack - How can I get the current user state (offline/online/away/etc.)?
Code for to connect with ejabbered and login is

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;
}

I always get Presence unavailable but if i use below code it works correct

Presence userFromServer=connection.getRoster().getPresence(connection.getUser());

what am i doing wrong so how can i get other users Presence ?

Community
  • 1
  • 1
Raj
  • 844
  • 2
  • 11
  • 29

1 Answers1

1

You can get the availability of the user by using user id of a particular user

Presence availability = roster.getPresence(user);

here : user means user id

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • Presence userFromServer=connection.getRoster().getPresence(connection.getUser()); when i use this i see the value in connection.getUser()=151083569134563@acqut/Smack – Raj Oct 31 '14 at 09:48
  • what i have to pass to get the other user Presence state – Raj Oct 31 '14 at 09:49
  • See. here first you have to get all the users names and Ids which are available in this particular user.. then pass that id to get the presence of that user. – RajaReddy PolamReddy Oct 31 '14 at 09:52
  • You mean to say i can not find Presence state of user by direct giving their jid in above code – Raj Oct 31 '14 at 10:00
  • You can get the presence of that JID user which you gave in the user id section of Presence call. i mean to say if you wan to get the presence of all the user's related to that login user (like if you logged in fb you will no which of our friends are in online/offline ). – RajaReddy PolamReddy Oct 31 '14 at 10:02
  • Yes brother i get it but i don't know what you want to say by mean related i am doing this for single chat so just simply say do i have to pass the other user id here – Raj Oct 31 '14 at 10:06
  • No need to pass if you are using this for single ser. i thought you want get all friends status...for for miss communication – RajaReddy PolamReddy Oct 31 '14 at 10:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63997/discussion-between-raj-and-rajareddy-polamreddy). – Raj Oct 31 '14 at 10:13