我已经使用 asmack lib 和 openfire 创建了一个聊天简单的聊天应用程序,但是当我在模拟器上对其进行测试时,我总是无法获得存在状态,而我的 openfire 服务器上有相同的用户可用。
我已经测试了talk.google.com 服务器的代码,它可以无缝运行,但是当我用开火进行测试时,我总是会出现不可用状态。我尝试了各种方法,例如在 openfire 设置中将订阅选项设置为两者。
我编写了以下代码来设置存在状态:
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Online, Programmatically!");
presence.setPriority(24);
presence.setMode(Presence.Mode.available);
connection.sendPacket(presence);
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
但是,当我运行以下代码时,我总是会出现状态不可用:
Roster roster = connection.getRoster();
Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all);
roster.addRosterListener(new RosterListener() {
// Ignored events public void entriesAdded(Collection<String> addresses) {}
public void entriesDeleted(Collection<String> addresses) {}
public void entriesUpdated(Collection<String> addresses) {}
public void presenceChanged(Presence presence) {
System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
}
@Override
public void entriesAdded(Collection<String> arg0) {
// TODO Auto-generated method stub}
});
try { Thread.sleep(5000); }
catch (InterruptedException e) { }
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
Presence subscribed = new Presence(Presence.Type.subscribed);
subscribed.setTo(entry.getUser());
connection.sendPacket(subscribed);
Log.d("JWP", "RosterEntry " + entry);
Log.d("JWP", "User: " + entry.getUser());
Log.d("JWP", "Name: " + entry.getName());
Log.d("JWP", "Status: " + entry.getStatus());
Log.d("JWP", "Type: " + entry.getType());
Presence entryPresence = roster.getPresence(entry.getUser());
Log.d("JWP", "Presence Status: "+ entryPresence.getMode());
Log.d("JWP", "Presence Type: " + entryPresence.getType());
Presence.Type type = entryPresence.getType();
if (type == Presence.Type.available){
Log.d("JWP", "Presence AVIALABLE");
Log.d("JWP", "Presence : " + entryPresence);
}
}
为什么会发生这种情况?我尝试了不同的方法,但我还没有找到确切的解决方案。