[英]Java smack / Android asmack RosterListener
I've been searching alot. 我一直在搜寻。 but couldn't find anything.
但找不到任何东西。 I need to refresh state of the contacts with rosterlistener.
我需要使用rosterlistener刷新联系人的状态。 (offline/online).
(离线/在线)。 WHen creating roster i do:
创建名单时,我会这样做:
Presence presence = roster.getPresence(r.getUser());
if(presence.isAvailable()){
userstatus.add("online");
}else{
userstatus.add("offline");
}
userstatus is a vector string, then i make a string array from it. userstatus是一个向量字符串,然后我从中创建一个字符串数组。 In my main program i just check this stirng array for the offline or online.
在我的主程序中,我只是在离线或在线检查此搅拌阵列。 But what to do if user goes offline/online.
但是如果用户离线/在线该怎么办。 WHere exactly i must put the listener and how exactly to use it?
我到底在哪里必须放置监听器,以及如何正确使用它?
You implement a RosterListener
and make use of the presenceChanged()
method. 您实现一个
RosterListener
并利用RosterListener
presenceChanged()
方法。 Here's sample code exactly as it appears in the Smack documentation . 这里的示例代码与Smack文档中显示的完全一样。
Roster roster = con.getRoster();
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);
}
});
Note that the JavaDoc suggests not using the presence received in the change event but checking for the best presence available each time: 请注意,JavaDoc建议不要使用在change事件中收到的状态,而是每次检查最佳状态:
To get the current "best presence" for a user after the presence update, query the roster:
要在状态更新后获得用户的当前“最佳状态”,请查询名册:
String user = presence.getFrom();
Presence bestPresence = roster.getPresence(user);
That will return the presence value for the user with the highest priority and availability.
这将为优先级和可用性最高的用户返回状态值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.