我可以从openfire获得所有注册用户,但无法获得所有用户的状态。我已经尝试了以下代码,但无论该用户是在线还是离线,它都会一直返回。请帮助我获取所有用户的状态。任何建议都可以接受。
try
{
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
myXMPP.getConnection().login(username, password);
UserSearchManager usm = new UserSearchManager(myXMPP.getConnection());
Form searchForm = usm.getSearchForm("search." + myXMPP.getConnection().getServiceName());
Form answerForm = searchForm.createAnswerForm();
UserSearch userSearch = new UserSearch();
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", "*");
ReportedData data = userSearch.sendSearchForm(myXMPP.getConnection(), answerForm, "search." + myXMPP.getConnection().getServiceName());
if (data.getRows() != null)
{
UserInfoDtl contactDetail = new UserInfoDtl(context);
for(int i=0;i<data.getRows().size();i++)
{
ContentValues values = new ContentValues();
String Name = data.getRows().get(i).getValues("Name").toString().replace("[", "").replace("]", "");
String Jid = data.getRows().get(i).getValues("jid").toString().replace("[", "").replace("]", "");
String Email = data.getRows().get(i).getValues("Email").toString().replace("[", "").replace("]", "");
String Status = "";
values.put(contactDetail.NAME,Name);
values.put(contactDetail.JID,Jid);
values.put(contactDetail.STATUS,Status);
values.put(contactDetail.EMAIL,Email);
// contactDetail.deleteTable();
if(!contactDetail.isUserExist(Jid))
contactDetail.insert(values);
Roster roster = Roster.getInstanceFor(myXMPP.getConnection());
/*Presence availability = roster.getPresence(Jid);
myXMPP.getConnection().sendStanza(availability);
Presence.Mode userMode = availability.getMode();
int flag = retrieveState_mode(availability.getMode(),availability.isAvailable());
Log.e(Jid,availability.isAvailable()+"");*/
try
{
Presence availability = roster.getPresence(Jid);
myXMPP.getConnection().sendStanza(availability);
Presence.Mode userMode = availability.getMode();
int flag = retrieveState_mode(availability.getMode(),availability.isAvailable());
Log.e(Jid,availability.getType()+"");
} catch (Exception e) {
android.util.Log.e("tag", "unable to add contact: ", e);
}
}
/*Roster roster = Roster.getInstanceFor(myXMPP.getConnection());
Presence availability = roster.getPresence(myXMPP.getConnection().getUser());
Log.e("Status" , availability.getType()+"");*/
/*Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
Presence presence = roster.getPresence(entry.getUser());
Log.e("Entry",entry+"");
Log.e("IsAvailable ",""+presence.isAvailable());
Log.e("Status" , ""+presence.getStatus());
}*/
}
return true;
}
catch (XMPPException | SmackException | IOException e)
{
if(e.getMessage().contains("Already Logged in"))
{
Log.e("Messaging Service","Already Logged in "+ myXMPP.getConnection().getUser());
return true;
}
else
{
ERRORMSG = e.getMessage();
return false;
}
}
//-------------------------------------------------------------
public static int retrieveState_mode(Presence.Mode userMode, boolean isOnline)
{
int userState = 0;
/** 0 for offline, 1 for online, 2 for away,3 for busy*/
if(userMode == Presence.Mode.dnd) {
userState = 3;
} else if (userMode == Presence.Mode.away || userMode == Presence.Mode.xa) {
userState = 2;
} else if (isOnline) {enter code here
userState = 1;
}
return userState;
}