下面是我添加的另一位朋友在我的應用程序。
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 {
// final String name = parseAddressName(address);
// Log.v(tag, msg)
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();
正確的順序:
- 用戶1送訂閱用戶2。
- 用戶2發送訂閱並被子訂回給用戶1。
- 用戶1發送Subsribed到user2。
另一個SO question,您可以檢查
我使用這個代碼roster.createEntry(地址,姓名,團體);仍然我正在鍵入type = none。如何在創建條目完成後使用上述存在代碼.. –
我不認爲訂單是問題。因爲當你添加一個聯繫人時,它與創建你的名單中的Entry相同併發送一個提交請求。 –
我已更新我的代碼與新添加的代碼如何添加條目,這些更改後,我也得到存在類型=無。 –