如何知道使用Smack的XMPP openfire中的打字状态

我正在使用Openfire XMPP服务器开发聊天应用程序。 我可以两个用户之间的文字聊天。 但是我想知道打字时的状态。 所以我创build了一个类:

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中使用它。 但我无法find这个数据包。

请有人build议,它将如何工作?

还有Smack和asmack有什么区别?

谢谢!

要启用ChatStateListener您需要创build一个自定义的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()); } } } 

然后你创buildMessageListener对象

 MessageListener messageListener = new MessageListenerImpl(); 

然后在创build聊天方法中传递

 Chat newChat = chatmanager.createChat(jabber_id_of_friend, messageListener); 

Smack和asmack有什么区别? < – 检查这个

最后我得到了解决scheme。 我需要与聊天pipe理器一起使用聊天监听器,并且还需要在构build的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的types。 参考: http : //www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smackx/MessageEventManager.html

 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所以会工作

我正在使用聊天状态侦听器:

 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"); } } } } 

您或您使用的另一个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(); } 

创build类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(“user2 @ localhost”),mMessageListener);

通过edittext文本更改将状态发送给接收用户

ChatStateManager.getInstance(connection).setCurrentState(ChatState.composing,Mychat);

这对我来说很好。

但是您也可以从ProcessPacket中获取它。 那里你会得到一个消息对象,你可以从那里提取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 } 

现在按照您的要求处理。

ChatStateManager之后,只需添加ChatStateManager:

 chatManager = ChatManager.getInstanceFor(getXmpptcpConnection()); ChatStateManager.getInstance(getXmpptcpConnection()); 

然后,您需要在createChat(to,chatMesageListener)期间添加ChatStateListener

 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()); } };