4

我是 android 和 xmpp 的新手。我正在尝试使用 xmpp 构建一个聊天应用程序。我想知道如何设置我的存在并让其他用户在 xmpp android 中存在。我尝试使用他们提供的文档,但它不起作用..总是状态显示为 null 并且类型为不可用。请发布详细答案。

  Roster roster = Roster.getInstanceFor(connection);    
//Get all rosters
if (!roster.isLoaded())
  try {
            roster.reloadAndWait();
        } catch (SmackException.NotLoggedInException |        SmackException.NotConnectedException | InterruptedException e) {
            e.printStackTrace();
        }
    Collection<RosterEntry> entries = roster.getEntries();
//loop through
    for (RosterEntry entry : entries) {
//example: get presence, type, mode, status
        Presence entryPresence = roster.getPresence(entry.getUser());
        StatusMode statusMode= StatusMode.createStatusMode(entryPresence);
//            statusMode.getMode();
        Presence.Type userType = entryPresence.getType();
        Presence.Mode mode = entryPresence.getMode();
        String status = entryPresence.getStatus();
        retrieveState_mode(mode,entryPresence.isAvailable());
        Log.d("####User status","...."+entry.getUser()+"....."+statusMode+"....."+entryPresence +" \ntype: "+"\n"+userType + "\nmode: " +mode + "\nstatus: " + status);// + "\nType: " + status.getType());
    }
    roster.addRosterListener(new RosterListener() {
        // Ignored events public void entriesAdded(Collection<String> addresses) {}
        public void entriesDeleted(Collection<String> addresses) {
        }

        @Override
        public void entriesAdded(Collection<String> addresses) {
        }

        public void entriesUpdated(Collection<String> addresses) {
        }

        public void presenceChanged(Presence presence) {
            System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
        }
    });
4

2 回答 2

1

有关文档,请参阅此处有关Xmpp此处了解详细信息

这里是查找用户状态的代码

 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;
}
于 2015-12-19T06:35:57.687 回答
0

首先,请按照以下步骤操作:

  1. 安装插件:presence
  2. Server settings > Presence service > Presence visibility
    然后单击Anyone,因为任何人都可能获得存在信息。

然后,实现以下代码:

public void setUserPresence(int code) {
    try {

        /** 5 for offline, 0,1 for online, 3 for away,2 for busy*/
        if (Config.conn1 == null)
            return;
        Presence presence;

        if (Config.conn1 != null) {
            Roster roster = Roster.getInstanceFor(Config.conn1);
            roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);

         roster.createEntry(sharedPreferenceManager.getUsername() + "@" + Config.openfire_host_server_SERVICE, sharedPreferenceManager.getUsername(), null);
        }
        switch (code) {
            case 0:


             /*   Presence pres = new Presence(Presence.Type.subscribe);
                pres.setFrom("userA@domain");
                Config.conn1.sendStanza(pres);*/

             Config.conn1.sendPacket(new Presence(Presence.Type.available));

                presence = new Presence(Presence.Type.available);
                presence.setStatus(Constants.ONLINE);
                presence.setPriority(24);
                presence.setMode(Presence.Mode.available);
                //roster = Roster.getInstanceFor(Config.conn1);
                Config.conn1.sendStanza(presence);

                Log.e("state", "Set online");

                break;
            case 1:
                presence = new Presence(Presence.Type.available);
                presence.setMode(Presence.Mode.chat);
                Config.conn1.sendStanza(presence);
                Log.e("state", "Set Q to me");
                System.out.println(presence.toXML());
                break;
            case 2:
                presence = new Presence(Presence.Type.available);
                presence.setMode(Presence.Mode.dnd);
                Config.conn1.sendStanza(presence);
                Log.e("state", "Set the busy");
                System.out.println(presence.toXML());
                break;
            case 3:
                presence = new Presence(Presence.Type.available);
                presence.setMode(Presence.Mode.away);
                Config.conn1.sendStanza(presence);
                Log.e("state", "Set off");
                System.out.println(presence.toXML());
                break;
            case 4:
                Roster roster1 = Roster.getInstanceFor(Config.conn1);
                Collection<RosterEntry> entries = roster1.getEntries();
                for (RosterEntry entry : entries) {
                    presence = new Presence(Presence.Type.unavailable);
                    // presence.setPacketID(Packet.ID_NOT_AVAILABLE);
                    presence.setFrom(Config.conn1.getUser());
                    presence.setTo(entry.getUser());
                    Config.conn1.sendStanza(presence);
                    System.out.println(presence.toXML());
                }
                // To the other client sends the same user stealth
                presence = new Presence(Presence.Type.unavailable);
                // presence.setPacketID(Packet.ID_NOT_AVAILABLE);
                presence.setFrom(Config.conn1.getUser());
                //presence.setTo(StringUtils.parseBareAddress(Config.conn1.getUser()));
                Config.conn1.sendPacket(presence);
                Log.e("state", "Set up stealth");
                break;
            case 5:
                presence = new Presence(Presence.Type.unavailable);
                presence.setStatus(Constants.OFFLINE);
                presence.setPriority(24);
                presence.setMode(Presence.Mode.away);
                Config.conn1.sendStanza(presence);
                Log.e("state", "Set offline");
                break;
            default:
                break;
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, "presence=" + e.toString());
    }
}

使用名册获取用户存在

Roster roster = Roster.getInstanceFor( conn1);
 Presence presence; presence = roster.getPresence(users.get(i).getUser()); 
 Log.e("Tag","presence.getStatus()>"+presence.getStatus());
于 2018-01-16T09:11:10.713 回答