これが私のアプリケーションに別の友達を追加する方法です。
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 {
roster.createEntry(address, name, groups);
// If contact exists locally, don't create another copy
Contact contact = makeContact(name, address);
if (!containsContact(contact))
notifyContactListUpdated(list,
ContactListListener.LIST_CONTACT_ADDED, contact);
else
debug(TAG, "skip adding existing contact locally " + name);
} catch (XMPPException e) {
throw new RuntimeException(e);
}
}
本質的な部分を使用するだけです
Presence response = new Presence.Type.subscribed);
response.setTo(address);
sendPacket(response);
Roster roster = mConnection.getRoster();
roster.createEntry(address, name, groups);
着信リクエストを聞くにaddPacketListener
は、接続に登録してください
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();
正しい順序:
- User1はuser2にサブスクライブを送信します。
- User2はSubscribeとSubsribedをuser1に送り返します。
- User1はuser2にサブスクライブを送信します。
あなたがチェックできるもう一つのSOの質問