2014-10-31 64 views
0

我用這個XMPP aSmack - How can I get the current user state (offline/online/away/etc.)?
代碼與ejabbered和登錄連接是安卓與Asmack - 如何檢查用戶在XMPP在android中的狀態?

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()); 

我在做什麼錯所以我如何獲得其他用戶存在?

回答

1

您可以通過使用特定的用戶

Presence availability = roster.getPresence(user); 

這裏的用戶ID獲取用戶的可用性:用戶是指用戶ID

+0

存在userFromServer = connection.getRoster()getPresence(connection.getUser。 ());當我使用這個我看到的值connection.getUser()= 151083569134563 @ acqut/Smack – Raj 2014-10-31 09:48:51

+0

我必須通過獲取其他用戶存在狀態 – Raj 2014-10-31 09:49:25

+0

請參閱。首先你必須得到這個特定用戶可用的所有用戶名和ID ..然後傳遞該ID來獲得該用戶的存在。 – 2014-10-31 09:52:01