This is how I add another friend to my application.
protected void doAddContactToListAsync(String address, String name, ContactList list) throws ImException { debug(TAG, "add contact to " + list.getName()); Presence response = new Presence.Type.subscribed); response.setTo(address); sendPacket(response); Roster roster = mConnection.getRoster(); String[] groups = new String[] { list.getName() }; if (name == null) { name = parseAddressName(address); } try {
Just use the important part
Presence response = new Presence.Type.subscribed); response.setTo(address); sendPacket(response); Roster roster = mConnection.getRoster(); roster.createEntry(address, name, groups);
To listen to an incoming request, register addPacketListener with your connection.
mConnection.addPacketListener(new PacketListener() { @Override public void processPacket(Packet packet) { Presence presence = (Presence) packet; if (presence.getType() == Type.subscribe) { debug(TAG, "sub request from 1" + address); //Implement accept or reject depend on user action. mContactListManager.getSubscriptionRequestListener() .onSubScriptionRequest(contact); //or you can test with send back Presence.subscribe first and send Presence.subscribed back to requester. } else {// Handle other Presence type. int type = parsePresence(presence); debug(TAG, "sub request from " + type); contact.setPresence(new Presence(type, presence.getStatus(), null, null, Presence.CLIENT_TYPE_DEFAULT)); } } }, new PacketTypeFilter(Presence.class)); mConnection.connect();
The correct order is:
- User1 send Subscribe to user2.
- User2 sends a subscription and subscribes to user1.
- Transfer User1 Subscribed to user2.
Another SO question you can check
Nam trung
source share