gpt4 book ai didi

android - 如何在xmpp android中设置和获取状态

转载 作者:行者123 更新时间:2023-11-29 01:25:24 24 4
gpt4 key购买 nike

我是 android 和 xmpp 的新手..我正在尝试使用 xmpp 构建一个聊天应用程序。我想知道如何在 xmpp android 中设置我的状态以及其他用户的状态。我尝试使用他们提供的文档,但它不起作用..总是状态显示为空,类型为不可用。请发布详细答案。

  Roster roster = Roster.getInstanceFor(connection);    
//Get all rosters
if (!roster.isLoaded())
try {
roster.reloadAndWait();
} catch (SmackException.NotLoggedInException | SmackException.NotConnectedException | InterruptedException e) {
e.printStackTrace();
}
Collection<RosterEntry> entries = roster.getEntries();
//loop through
for (RosterEntry entry : entries) {
//example: get presence, type, mode, status
Presence entryPresence = roster.getPresence(entry.getUser());
StatusMode statusMode= StatusMode.createStatusMode(entryPresence);
// statusMode.getMode();
Presence.Type userType = entryPresence.getType();
Presence.Mode mode = entryPresence.getMode();
String status = entryPresence.getStatus();
retrieveState_mode(mode,entryPresence.isAvailable());
Log.d("####User status","...."+entry.getUser()+"....."+statusMode+"....."+entryPresence +" \ntype: "+"\n"+userType + "\nmode: " +mode + "\nstatus: " + status);// + "\nType: " + status.getType());
}
roster.addRosterListener(new RosterListener() {
// Ignored events public void entriesAdded(Collection<String> addresses) {}
public void entriesDeleted(Collection<String> addresses) {
}

@Override
public void entriesAdded(Collection<String> addresses) {
}

public void entriesUpdated(Collection<String> addresses) {
}

public void presenceChanged(Presence presence) {
System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
}
});

最佳答案

关于 Xmpp 的文件请引用这里和 here for details

这里是查找用户状态的代码

 Presence availability = roster.getPresence(user);
Mode userMode = availability.getMode();

retrieveState_mode(availability.getMode(),availability.isAvailable());

public static int retrieveState_mode(Mode userMode, boolean isOnline) {
int userState = 0;
/** 0 for offline, 1 for online, 2 for away,3 for busy*/
if(userMode == Mode.dnd) {
userState = 3;
} else if (userMode == Mode.away || userMode == Mode.xa) {
userState = 2;
} else if (isOnline) {
userState = 1;
}
return userState;
}

关于android - 如何在xmpp android中设置和获取状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34367920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com