如何在 xmpp android 中设置和获取状态
Posted
技术标签:
【中文标题】如何在 xmpp android 中设置和获取状态【英文标题】:How to set and get presence in xmpp android 【发布时间】:2015-12-19 06:15:03 【问题描述】:我是 android 和 xmpp 的新手..我正在尝试使用 xmpp 构建一个聊天应用程序。我想知道如何设置我的存在并让其他用户在 xmpp android 中存在。我尝试使用他们提供的文档,但它不起作用..总是状态显示为 null 并且类型为不可用。请发布详细答案。
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);
);
【问题讨论】:
请添加一些您尝试过的代码 - 这样更容易帮助您 @Cory 添加了代码.. 【参考方案1】:关于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;
【讨论】:
您好,感谢@sasikumar 的回复...我已经尝试过了,但我得到的“isOnline”始终是错误的【参考方案2】:首先,请按照以下步骤操作:
-
安装插件:
presence
转到Server settings > Presence service > Presence visibility
。
然后点击Anyone
,因为任何人都可以获取在线信息。
然后,实现以下代码:
public void setUserPresence(int code)
try
/** 5 for offline, 0,1 for online, 3 for away,2 for busy*/
if (Config.conn1 == null)
return;
Presence presence;
if (Config.conn1 != null)
Roster roster = Roster.getInstanceFor(Config.conn1);
roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
roster.createEntry(sharedPreferenceManager.getUsername() + "@" + Config.openfire_host_server_SERVICE, sharedPreferenceManager.getUsername(), null);
switch (code)
case 0:
/* Presence pres = new Presence(Presence.Type.subscribe);
pres.setFrom("userA@domain");
Config.conn1.sendStanza(pres);*/
Config.conn1.sendPacket(new Presence(Presence.Type.available));
presence = new Presence(Presence.Type.available);
presence.setStatus(Constants.ONLINE);
presence.setPriority(24);
presence.setMode(Presence.Mode.available);
//roster = Roster.getInstanceFor(Config.conn1);
Config.conn1.sendStanza(presence);
Log.e("state", "Set online");
break;
case 1:
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.chat);
Config.conn1.sendStanza(presence);
Log.e("state", "Set Q to me");
System.out.println(presence.toXML());
break;
case 2:
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.dnd);
Config.conn1.sendStanza(presence);
Log.e("state", "Set the busy");
System.out.println(presence.toXML());
break;
case 3:
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.away);
Config.conn1.sendStanza(presence);
Log.e("state", "Set off");
System.out.println(presence.toXML());
break;
case 4:
Roster roster1 = Roster.getInstanceFor(Config.conn1);
Collection<RosterEntry> entries = roster1.getEntries();
for (RosterEntry entry : entries)
presence = new Presence(Presence.Type.unavailable);
// presence.setPacketID(Packet.ID_NOT_AVAILABLE);
presence.setFrom(Config.conn1.getUser());
presence.setTo(entry.getUser());
Config.conn1.sendStanza(presence);
System.out.println(presence.toXML());
// To the other client sends the same user stealth
presence = new Presence(Presence.Type.unavailable);
// presence.setPacketID(Packet.ID_NOT_AVAILABLE);
presence.setFrom(Config.conn1.getUser());
//presence.setTo(StringUtils.parseBareAddress(Config.conn1.getUser()));
Config.conn1.sendPacket(presence);
Log.e("state", "Set up stealth");
break;
case 5:
presence = new Presence(Presence.Type.unavailable);
presence.setStatus(Constants.OFFLINE);
presence.setPriority(24);
presence.setMode(Presence.Mode.away);
Config.conn1.sendStanza(presence);
Log.e("state", "Set offline");
break;
default:
break;
catch (Exception e)
e.printStackTrace();
Log.e(TAG, "presence=" + e.toString());
使用名册获取用户存在
Roster roster = Roster.getInstanceFor( conn1);
Presence presence; presence = roster.getPresence(users.get(i).getUser());
Log.e("Tag","presence.getStatus()>"+presence.getStatus());
【讨论】:
//使用名册获取用户存在情况 Roster roster = Roster.getInstanceFor(conn1);存在存在;存在 = roster.getPresence(users.get(i).getUser()); Log.e("Tag","presence.getStatus()>"+presence.getStatus());以上是关于如何在 xmpp android 中设置和获取状态的主要内容,如果未能解决你的问题,请参考以下文章
如何在 angular2-localstorage 中设置和获取?
如何在 Azure Synapse 或数据工厂管道中设置和获取变量值