我正在使用Openfire XMPP服务器开发聊天应用程序。我可以在两个用户之间进行文字聊天但是我想在某人输入消息时知道打字状态。所以我创建了一个类: -
public class typingStatus implements ChatStateListener {
@Override
public void processMessage(Chat arg0, Message arg1) {
// TODO Auto-generated method stub
}
@Override
public void stateChanged(Chat arg0, ChatState arg1) {
// TODO Auto-generated method stub
System.out.println(arg0.getParticipant() + " is " + arg1.name());
}
}
但我很困惑,它将如何运作?我知道我需要一个数据包,我可以在Listener中使用它。但我无法找到那个数据包。
请任何人建议,它将如何运作?
还有Smack和asmack有什么区别?
谢谢!
答案 0 :(得分:12)
要启用ChatStateListener
,您需要创建自定义MessageListener
类
public class MessageListenerImpl implements MessageListener,ChatStateListener {
@Override
public void processMessage(Chat arg0, Message arg1) {
System.out.println("Received message: " + arg1);
}
@Override
public void stateChanged(Chat arg0, ChatState arg1) {
if (ChatState.composing.equals(arg1)) {
Log.d("Chat State",arg0.getParticipant() + " is typing..");
} else if (ChatState.gone.equals(arg1)) {
Log.d("Chat State",arg0.getParticipant() + " has left the conversation.");
} else {
Log.d("Chat State",arg0.getParticipant() + ": " + arg1.name());
}
}
}
然后创建MessageListener
对象
MessageListener messageListener = new MessageListenerImpl();
然后在创建聊天方法
中传递此内容Chat newChat = chatmanager.createChat(jabber_id_of_friend, messageListener);
Smack和asmack有什么区别?< - Check This
答案 1 :(得分:5)
最后我得到了解决方案。我需要使用聊天监听器和聊天管理器,我还需要使用内置的 sendcomposingnotification 功能。无需为此使用Messageeventrequestlistener接口或任何其他自定义类。我添加了以下几行,
connection.getChatManager().addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(final Chat arg0, final boolean arg1) {
// TODO Auto-generated method stub
arg0.addMessageListener(new MessageListener()
{
@Override
public void processMessage(Chat arg0, Message arg1)
{
// TODO Auto-generated method stub
Log.d("TYpe Stat",title[0] + " is typing......");
Toast.makeText(getApplicationContext(),title[0] + " is typing......",Toast.LENGTH_SHORT).show();
}
}
});
}
});
并且还需要发送这样的通知..
mem.sendComposingNotification(etRecipient.getText().toString(), message.getPacketID());
System.out.println("Sending notification");
其中mem是MessageEventManger的类型。 参考:http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smackx/MessageEventManager.html
答案 2 :(得分:2)
ChatManager chatManager = ChatManager.getInstanceFor(connection);
Chat chat= chatManager.createChat(to, new ChatStateListener() {
@Override
public void stateChanged(Chat chat, ChatState state) {
switch (state){
case active:
Log.d("state","active");
break;
case composing:
Log.d("state","composing");
break;
case paused:
Log.d("state","paused");
break;
case inactive:
Log.d("state","inactive");
break;
case gone:
Log.d("state","gone");
break;
}
}
@Override
public void processMessage(Chat chat, Message message) {
Log.d("processMessage","processMessage");
}
});
使用此code.hope以便工作
答案 3 :(得分:1)
我正在使用聊天状态监听器:
Chat chat = chatManager.createChat(jid,
new ChatStateChangedListener());
将chatstatelistener与上面的每个jid绑定,然后:
public class ChatStateChangedListener implements ChatStateListener {
public ChatStateChangedListener() {
printLog("Chat State Changed Listner Constructor");
}
@Override
public void processMessage(Chat arg0, Message arg1) {
}
@Override
public void stateChanged(Chat chat, ChatState state) {
if (state.toString().equals(ChatState.composing.toString())) {
tvLastSeen.setText("Typing...");
} else if (state.toString().equals(ChatState.paused.toString())) {
tvLastSeen.setText("paused...");
} else {
tvLastSeen.setText("nothing");
}
}
}
}
答案 4 :(得分:1)
创建On Class MMessageListener以侦听传入的消息
私有类MMessageListener实现了MessageListener,ChatStateListener { public MMessageListener(Context contxt) {
}
@Override
public void stateChanged(Chat chat, ChatState chatState) {
mStatus = "Online";
if (ChatState.composing.equals(chatState)) {
mStatus = chat.getParticipant() + " is typing..";
Log.d("Chat State", chat.getParticipant() + " is typing..");
} else if (ChatState.gone.equals(chatState)) {
Log.d("Chat State", chat.getParticipant() + " has left the conversation.");
mStatus = chat.getParticipant() + " has left the conversation.";
} else if (ChatState.paused.equals(chatState)){
Log.d("Chat State", chat.getParticipant() + ": " + chatState.name());
mStatus = "Paused";
}else if (ChatState.active.equals(chatState)){
mStatus = "Online";
}
// do whatever you want to do once you receive status
}
@Override
public void processMessage(Message message) {
}
@Override
public void processMessage(Chat chat, Message message) {
}
}
向您的聊天对象添加监听器
聊天Mychat = ChatManager.getInstanceFor(连接).createChat( “用户2 @ localhost” 的), mMessageListener);
在edittext文本更改时向接收用户发送状态
ChatStateManager.getInstance(connection).setCurrentState(ChatState.composing,Mychat);
这对我来说很好。
答案 5 :(得分:0)
你或你使用的另一个xmpp客户端应该发送聊天状态你可以抓住状态。
喜欢这个;
try {
ChatStateManager.getInstance(GlobalVariables.xmppManager.connection).setCurrentState(ChatState.composing, chat);
} catch (XMPPException e) {
e.printStackTrace();
}
或
try {
ChatStateManager.getInstance(GlobalVariables.xmppManager.connection).setCurrentState(ChatState.gone, chat);
} catch (XMPPException e) {
e.printStackTrace();
}
答案 6 :(得分:0)
但是你也可以从ProcessPacket获得它。 在那里你将获得一个Message对象,之后你可以从那里提取xml部分并处理它们包含特定的聊天状态。
Message message = (Message) packet;
String msg_xml = message.toXML().toString();
if (msg_xml.contains(ChatState.composing.toString())) {
//handle is-typing, probably some indication on screen
} else if (msg_xml.contains(ChatState.paused.toString())) {
// handle "stopped typing"
} else {
// normal msg
}
现在根据您的要求处理。
答案 7 :(得分:0)
只需在ChatStateManager
intalization之后添加ChatManager
:
chatManager = ChatManager.getInstanceFor(getXmpptcpConnection());
ChatStateManager.getInstance(getXmpptcpConnection());
然后您需要在ChatStateListener
期间添加createChat(to,chatMesageListener)
:
chatManager.createChat(message.getTo(), chatMessageListener).sendMessage(message);
private ChatStateListener chatMessageListener = new ChatStateListener() {
@Override
public void stateChanged(Chat chat, ChatState state) {
//State Change composing,active,paused,gone,etc
Log.d(TAG, "ChatStateListener:::stateChanged -> " + chat.toString() + " \n -> " + state.toString());
}
@Override
public void processMessage(Chat chat, Message message) {
//Incoming Message
Log.d(TAG, "ChatStateListener:::processMessage -> " + chat.toString() + " \n -> " + message.toString());
}
};