我在android中使用openfire服务器。现在我需要检查有多少用户在线以及他们是谁。我的联系还可以。但我需要添加或成为这些用户的朋友。怎么解决?请有人帮助我。
输出如
更改名单条目的存在时调用
姓名:esty
状态:不可用
更改名单条目的存在时调用
姓名:roni
状态:不可用
这是我的代码:
public void OnlineUser()
{
final Roster sroster = connection.getRoster();
try {
sroster.createEntry("esty", "test", null);
} catch (XMPPException e) {
e.printStackTrace();
}
final Presence pr = new Presence(Presence.Type.available);
connection.sendPacket(pr);
// context.setConnection(Constants.connection);
// final Roster roster =connection.getRoster();
final Collection<RosterEntry> entries = sroster.getEntries();
sroster.addRosterListener(new RosterListener() {
@Override
public void presenceChanged(Presence presence) {
//Called when the presence of a roster entry is changed
for (RosterEntry entry : entries) {
if(pr.equals( presence)){
Log.v("roni", "Called when the presence of a roster entry is changed ");
Presence entryPresence = sroster.getPresence(entry.getUser());
Presence.Type type = entryPresence.getType();
uName =entry.getName().toString();
uStatus=type.toString();
Log.v("roni", "Name: " + entry.getName().toString());
Log.v("roni", "Status: " +type.toString());
}
}
}
@Override
public void entriesUpdated(Collection<String> arg0) {
// Called when a roster entries are updated.
for (RosterEntry entry : entries) {
Log.v("roni", "Called when a roster entries are updated ");
Presence entryPresence = sroster.getPresence(entry.getUser());
Presence.Type type = entryPresence.getType();
uName =entry.getName().toString();
uStatus=type.toString();
Log.v("roni", "Namne: " + entry.getName().toString());
Log.v("roni", "Status: " +type.toString());
}
}
@Override
public void entriesDeleted(Collection<String> arg0) {
// Called when a roster entries are removed.
for (RosterEntry entry : entries) {
Log.v("roni", "Called when a roster entries are removed. ");
Presence entryPresence = sroster.getPresence(entry.getUser());
Presence.Type type = entryPresence.getType();
uName =entry.getName().toString();
uStatus=type.toString();
Log.v("roni", "Namne: " + entry.getName().toString());
Log.v("roni", "Status: " +type.toString());
}
}
@Override
public void entriesAdded(Collection<String> arg0) {
// Called when a roster entries are added.
for (RosterEntry entry : entries) {
Log.v("roni", " Called when a roster entries are added. ");
Presence entryPresence = sroster.getPresence(entry.getUser());
Presence.Type type = entryPresence.getType();
uName =entry.getName().toString();
uStatus=type.toString();
Log.v("roni", "Namne: " + entry.getName().toString());
Log.v("roni", "Status: " +type.toString());
}
}
});
}