XMPP aSmack - 如何获取当前用户状态(离线/在线/离开/等)?

时间:2013-01-22 06:51:35

标签: android xmpp smack asmack

我是新手中的xmpp / asmack。

任何人都可以帮助我获得用户朋友的存在(名单)

我正在使用它:

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

我还应该做些什么来获取我名单中列出的每个用户的可用性状态。

5 个答案:

答案 0 :(得分:18)

就这样使用:

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

如果您对xmpp / asmack

有任何疑问,请与我们联系

答案 1 :(得分:3)

像这样使用

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

答案 2 :(得分:1)

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

                }
            }

        }

如果您使用RosterListener,它会实时更新状态,它对我来说很合适。

答案 3 :(得分:0)

ConnectToServer(){
    final ProgressDialog dialog = ProgressDialog.show(ChatWindowFragmentActivity.this,
            "Connecting...", "Please wait...", false);
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {


            // Object of XmppClient class
            XmppClient mXmppClient = new XmppClient();

            /*
             * // Create a connection ConnectionConfiguration connConfig =
             * new ConnectionConfiguration(HOST, PORT);
             */
            XMPPConnection connection = null;

            try {
                SmackAndroid.init(ChatWindowFragmentActivity.this);
                connection = mXmppClient.connectionToXmppServer();
            } catch (XMPPException e) {
                // TODO Auto-generated catch block
      //                    setConnection(null, null);
            }
            try {
                mXmppClient.loginUser(connection, USERNAME, PASSWORD);
                Log.i("XMPPChatDemoActivity",
                        "Logged in as" + connection.getUser());
                // Set the status to available
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);

                setConnection(connection);

                Roster roster = connection.getRoster();

                 /*
                Fetch USER availability
                */


                switch (isUserAvailable(connection)){
                    case 0:
                        imgAvailability.setBackgroundColor(Color.GRAY);
                        break;
                    case 1:
                        imgAvailability.setBackgroundColor(Color.GREEN);
                        break;
                    case 2:
                        imgAvailability.setBackgroundColor(Color.YELLOW);
                        break;
                    case 3:
                        imgAvailability.setBackgroundColor(Color.RED);
                        break;
                    default:
                        break;
                }


                Collection<RosterEntry> entries = roster.getEntries();
                for (RosterEntry entry : entries) {

                    Log.d("XMPPChatDemoActivity",
                            "--------------------------------------");
                    Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
                    Log.d("XMPPChatDemoActivity",
                            "User: " + entry.getUser());
                    Log.d("XMPPChatDemoActivity",
                            "Name: " + entry.getName());
                    Log.d("XMPPChatDemoActivity",
                            "Status: " + entry.getStatus());
                    Log.d("XMPPChatDemoActivity",
                            "Type: " + entry.getType());
                    Presence entryPresence = roster.getPresence(entry
                            .getUser());

                    Log.d("XMPPChatDemoActivity", "Presence Status: "
                            + entryPresence.getStatus());
                    Log.d("XMPPChatDemoActivity", "Presence Type: "
                            + entryPresence.getType());

                    Presence.Type type = entryPresence.getType();
                    if (type == Presence.Type.available)
                        Log.d("XMPPChatDemoActivity", "Presence AVAILABLE");
                    Log.d("XMPPChatDemoActivity", "Presence : "
                            + entryPresence);
                }



            } catch (XMPPException e) {

                e.printStackTrace();
                Log.e("XMPPChatDemoActivity", "Failed to log in as "
                        + USERNAME);
                Log.e("XMPPChatDemoActivity", e.toString());
                new ShowAlert(ChatWindowFragmentActivity.this,e.getMessage(), false).show(
                        getSupportFragmentManager(), TAG);
   //                    setConnection(null, null);
            }

            dialog.dismiss();
        }
    });
    t.start();
    dialog.show();
}

你的方法在里面调用。

答案 4 :(得分:0)

根据我之前的经验,您可以在Presence中查看状态和其他信息,您需要订阅用户。

例如: 用户A想要查看用户B的状态和可用状态, 在这种情况下,用户A需要订阅用户B.之后用户A可以看到用户B Presence。

订阅代码

try {
            roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
            String userName = responders.getUsers().get(i).getUsername();
            roster.createEntry("userB@domain", userName, null);
            Presence pres = new Presence(Presence.Type.subscribe);
            pres.setFrom("userA@domain");
            connection.sendStanza(pres);
        } catch (Exception e) {
            android.util.Log.e("tag", "unable to add contact: ", e);
        }