发送消息后,它正在接收到该组。但与此同时,我又收到了以前的所有消息。帮助我解决这个问题。 这是我的代码:
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
to = groupname + "@conference.localhost".toString();
String text = (textMessage.getText().toString());
textMessage.setText("");
muc = new MultiUserChat(connection, to);
try {
muc.join(USERNAME);
} catch (XMPPException e) {
e.printStackTrace();
}
Message msg = new Message(to, Message.Type.groupchat);
msg.setBody(text);
if (connection != null) {
connection.sendPacket(msg);
Msg data = new Msg();
data.setMessage(text);
data.setName(userFrom);
data.setDate(DateAndTime.getCurrentDate());
data.setTime(DateAndTime.getCurrentTime());
data.setSender(true);
MessageListAdapter.messagesItems.add(data);
notifyMyAdapter();
}
}
});
这是我的接收代码:
public void setConnection(XMPPConnection connection) {
this.connection = connection;
if(connection != null) {
PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("XMPPChatDemoActivity ", " Text Recieved " + message.getBody() + " from " + fromName);
Msg data = new Msg();
String subject = message.getSubject();
if (subject == null) {
data.setMessage(message.getBody());
data.setName(NAME);
data.setDate(DateAndTime.getCurrentDate());
data.setTime(DateAndTime.getCurrentTime());
data.setSender(false);
MessageListAdapter.messagesItems.add(data);
mHandler.post(new Runnable() {
@Override
public void run() {
notifyMyAdapter();
}
});
} else if (subject.equals("")){
data.setMessage(message.getBody());
data.setName(NAME);
data.setDate(DateAndTime.getCurrentDate());
data.setTime(DateAndTime.getCurrentTime());
data.setSender(false);
MessageListAdapter.messagesItems.add(data);
// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {
notifyMyAdapter();
}
});
}
}
}
},filter);
}
}
答案 0 :(得分:0)
在MultiUserChat
中要控制加入会议室时要接收的历史记录数量,您需要提供已配置的DiscussionHistory
对象。有关MultiUserChat
refer this
muc.join(String nickname, String password, DiscussionHistory history, long timeout)
其中DiscussionHistory
类控制字符数
或进入房间时收到的消息。房间将决定
如果您未指定a,则返回的历史记录数量
DiscussionHistory
加入一个房间。了解更多信息refer this
示例:
MultiUserChat muc = new MultiUserChat(connection, to);
DiscussionHistory history = new DiscussionHistory(); history.setMaxStanzas(0);
muc.join("username", "password", history, SmackConfiguration.getPacketReplyTimeout());