smack-android-4.1.9(不同的jar包获取的方法不同)
1.获取服务器上的所有聊天室
List<String> serviceNames =
multiUserChatManager.getServiceNames();
for (int
i =
0;
i<
serviceNames.size();
i++)
{
List<HostedRoom> hostedRooms = multiUserChatManager.getHostedRooms(serviceNames.get(i));
roomList.addAll(hostedRooms);
}
注意:servicename是通过multiUserChatManager获得的,不是通过String serviceName = connection.getServiceName();
2.创建聊天室
/**
* 创建群聊聊天室
*
* @param roomName 聊天室名字
* @param nickName 创建者在聊天室中的昵称
* @param password 聊天室密码
* @return
*/
public MultiUserChat
createChatRoom(String roomName, String nickName, String password) {
if (!iConnection.isConnected()) {
throw new NullPointerException(
"服务器连接失败,请先连接服务器");
}
MultiUserChat muc =
null;
try {
muc = MultiUserChatManager.getInstanceFor(connection).getMultiUserChat(roomName +
"@conference." + connection.getServiceName());
boolean isCreated = muc.createOrJoin(nickName);
if (isCreated) {
Form form = muc.getConfigurationForm();
Form submitForm = form.createAnswerForm();
List<FormField> fields = form.getFields();
for (
int i =
0; fields !=
null && i < fields.size(); i++) {
if (FormField.Type.hidden != fields.get(i).getType() &&
fields.get(i).getVariable() !=
null) {
submitForm.setDefaultAnswer(fields.get(i).getVariable());
}
}
List owners =
new ArrayList();
owners.add(connection.getUser());
submitForm.setAnswer(
"muc#roomconfig_roomowners", owners);
submitForm.setAnswer(
"muc#roomconfig_persistentroom",
true);
submitForm.setAnswer(
"muc#roomconfig_membersonly",
false);
submitForm.setAnswer(
"muc#roomconfig_allowinvites",
true);
if (password !=
null && password.length() !=
0) {
submitForm.setAnswer(
"muc#roomconfig_passwordprotectedroom",
true);
submitForm.setAnswer(
"muc#roomconfig_roomsecret", password);
}
submitForm.setAnswer(
"muc#roomconfig_enablelogging",
true);
submitForm.setAnswer(
"x-muc#roomconfig_reservednick",
true);
submitForm.setAnswer(
"x-muc#roomconfig_canchangenick",
false);
submitForm.setAnswer(
"x-muc#roomconfig_registration",
false);
muc.sendConfigurationForm(submitForm);
Toast.makeText(
this,
"创建成功", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(
this,
"创建失败", Toast.LENGTH_SHORT).show();
}
}
catch (XMPPException | SmackException e) {
e.printStackTrace();
Toast.makeText(
this,
"创建失败"+e.getMessage(),Toast.LENGTH_LONG).show();
return null;
}
return muc;
}
3.加入聊天室
MultiUserChat muc = MultiUserChatManager.getInstanceFor(connection).
getMultiUserChat(roomName +
"@conference." + connection.getServiceName());
DiscussionHistory history =
new DiscussionHistory();
history.setMaxChars(
0);
muc.
join(nickName, password);
转载请注明原文地址: https://ju.6miu.com/read-18809.html