How to set and get presence in xmpp android

ⅰ亾dé卋堺 提交于 2020-01-04 05:36:18

问题


I am new to android and xmpp..i am trying to build a chatting app using xmpp. I want to know How to set my presence and also get other users presence in xmpp android. i tried with their provided documentation but it is not working ..always status shows null and type as unavailable. please post a detail answer.

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

回答1:


for document refer here about Xmpp and here for details

Here the code for finding user state

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



回答2:


First of all, follow the steps bellow:

  1. Install the plugin: presence
  2. Go to Server settings > Presence service > Presence visibility.
    Then click on Anyone, as anyone may get presence information.

Then, implement the following code:

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

get user presence using roster

Roster roster = Roster.getInstanceFor( conn1);
 Presence presence; presence = roster.getPresence(users.get(i).getUser()); 
 Log.e("Tag","presence.getStatus()>"+presence.getStatus());


来源:https://stackoverflow.com/questions/34367920/how-to-set-and-get-presence-in-xmpp-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!