有两个用户 A 和 B 。
帮我了解一对一聊天的聊天记录?
这是发送消息:
public void sendTextMessage(View v) {
String message = msg_edittext.getEditableText().toString();
if (!message.equalsIgnoreCase("")) {
final ChatMessage chatMessage = new ChatMessage(user1, user2,
message, "" + random.nextInt(1000), false);
chatMessage.setMsgID();
chatMessage.body = message;
chatMessage.Date = CommonMethods.getCurrentDate();
chatMessage.Time = CommonMethods.getCurrentTime();
msg_edittext.setText("");
chatAdapter.add(chatMessage);
chatAdapter.notifyDataSetChanged();
//MainActivity activity = ((MainActivity) context);
getmService().xmpp.sendMessage(chatMessage);
}
}
public void sendMessage(ChatMessage chatMessage)
{
String body = gson.toJson(chatMessage);
if (!chat_created)
{
Mychat = ChatManager.getInstanceFor(connection).createChat(
chatMessage.receiver + "@"
+ "sspl163",
mMessageListener);
chat_created = true;
}
final Message message = new Message();
message.setBody(body);
message.setStanzaId(chatMessage.msgid);
message.setType(Message.Type.chat);
try {
if (connection.isAuthenticated())
Mychat.sendMessage(message);
else
login();
}
catch (NotConnectedException e) {
Log.e("xmpp.SendMessage()", "msg Not sent!-Not Connected!");
}
catch (Exception e) {}
}
这是用于检索消息:
private class MMessageListener implements ChatMessageListener
{
public MMessageListener(Context contxt) {}
@Override
public void processMessage(final org.jivesoftware.smack.chat.Chat chat, final Message message)
{
if (message.getType() == Message.Type.chat && message.getBody() != null)
{
final ChatMessage chatMessage = gson.fromJson(message.getBody(), ChatMessage.class);
processMessage(chatMessage);
}
}
private void processMessage(final ChatMessage chatMessage)
{
chatMessage.isMine = false;
SharedPreferences shared = context.getSharedPreferences("MyPREFERENCES", MODE_PRIVATE);
String user = (shared.getString("username", ""));
if(chatMessage.receiver.equalsIgnoreCase(user) && Chats.user2.equalsIgnoreCase(chatMessage.sender))
Chats.chatlist.add(chatMessage);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Chats.chatAdapter.notifyDataSetChanged();
}
});
}
}