嘿我正试图在另一个人开始写入时尝试输入状态,即使它返回一条状态为消息的空消息,但它来自proccessMessage方法。难道不能在州改变吗?我还没有启用任何PacketProvider,我是否必须这样做才能在stateChanged方法中返回?我将不胜感激任何帮助。
newChat = chatmanager.createChat(contactUsername + sc.DOMAIN, new ChatStateListener() {
@Override
public void stateChanged(Chat chat, ChatState state) {
if (ChatState.composing.equals(state)) {
Log.d("Chat State",chat.getParticipant() + " is typing..");
} else if (ChatState.gone.equals(state)) {
Log.d("Chat State",chat.getParticipant() + " has left the conversation.");
} else {
Log.d("Chat State",chat.getParticipant() + ": " + state.name());
}
}
@Override
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: " + message);
}
});
日志:
06-15 14:28:47.679 9949-9983/com.example.example I/System.out﹕ Received message: <message to='+931111111111@example.com' from='+930000000000@example.com/Spark 2.6.3' id='AUJG0-42' type='chat'><thread>097uC9</thread><inactive xmlns='http://jabber.org/protocol/chatstates'/></message>
答案 0 :(得分:8)
这是一个普遍的答案,可能会有更好的答案,但这肯定有效,它可能包含你已经知道的东西,但我想它不会受到伤害。
基本假设 - composing
消息是正文为空的消息,整个消息的xml包含ChatState
标记,其中包含active, composing, paused, inactive, gone
个值之一,以及您是什么寻找的是composing
,而它是对应的,paused
。
话虽如此,在接收组件上,你需要这样的东西(根据你的需要修改它):
yourComponent.addMessageListener(new PacketListener()
{
@Override
public void processPacket(Packet packet) throws NotConnectedException
{
try
{
Message msg = (Message) packet;
String msg_xml = msg.toXML().toString();
if (null != msg.getBody())
{
//handle as a regular chat message....
}
else
{
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"
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
编辑:
您的日志消息显示您收到了inactive
消息,因此对于初学者,请尝试过滤所有消息,并找到您要查找的模式,以查看发送方是否甚至发送你需要的东西。
理解的一个好方法是使用另一个你知道的客户端发送组合状态的事实,并看看你在过滤器中得到了什么(例如在linux下你可以使用pidgin或同理心来发送端)。
编辑2:
根据要求,我还添加了一些发送方组件以获得更完整的图片,同样,这是一个供参考的一般示例,可以进行优化和更改。
首先,实现一个自定义EditText,它实现一个TextWatcher,其中包含一个小线程,该线程会根据您上次文本更改所需的延迟触发“我已停止输入”,根据需要设置您的间隔:
public class MyIsTypingEditText extends EditText implements TextWatcher
{
private static final int TypingInterval = 800;
//your listener interface that you implement anonymously from the Activity
public interface OnTypingModified
{
public void onIsTypingModified(EditText view, boolean isTyping);
}
private OnTypingModified typingChangedListener;
//members you would need for the small thread that declares that you have stopped typing
private boolean currentTypingState = false;
private Handler handler = new Handler();
private Runnable stoppedTypingNotifier = new Runnable()
{
@Override
public void run()
{
//part A of the magic...
if(null != typingChangedListener)
{
typingChangedListener.onIsTypingModified(MyIsTypingEditText.this, false);
currentTypingState = false;
}
}
};
public MyIsTypingEditText(Context context)
{
super(context);
this.addTextChangedListener(this);
}
public void setOnTypingModified(OnTypingModified typingChangedListener)
{
this.typingChangedListener = typingChangedListener;
}
@Override
public void afterTextChanged(Editable s)
{
//part B of the magic...
if(null != typingChangedListener)
{
if(!currentTypingState)
{
typingChangedListener.onIsTypingModified(this, true);
currentTypingState = true;
}
handler.removeCallbacks(stoppedTypingNotifier);
handler.postDelayed(stoppedTypingNotifier, TypingInterval);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence text, int start, int before, int after) { }
}
接下来,在处理聊天的活动中,添加一个新的匿名接口,并使用状态更改消息发送方法调用实现它的回调,这是魔术的C部分,因为这将发送对应于TextWatcher的状态微型螺纹:
yourIsTypingEditText.setOnTypingModified(new MyIsTypingEditText.OnTypingModified()
{
@Override
public void onIsTypingModified(EditText view, boolean isTyping)
{
XmppConnectionClass.Composing composing = (isTyping) ? XmppConnectionClass.Composing.isTyping : XmppConnectionClass.Composing.stoppedTyping;
XmppConnectionClass.getInstance().sendIsComposing(composing);
}
});
最后但并非最不重要的是,在连接处理程序类中,您应该具有以下内容:
//.... other stuff
//a mini enum Composing class for state setting
public enum Composing
{
private ChatState state;
Composing(ChatState state)
{
this.state = state;
}
isTyping(ChatState.composing), stoppedTyping(ChatState.paused);
public ChatState getState()
{
return (state);
}
}
//.... other stuff
public boolean sendIsComposing(Composing composing)
{
try
{
Message msg = new Message(...);
//other stuff...
msg.setBody(null);
msg.addExtension(new ChatStateExtension(composing.getState()));
//other stuff...
this.sendMessage(msg);
return (true);
}
catch (Exception e)
{
e.printStackTrace();
return (false);
}
}
希望这有助于。
答案 1 :(得分:1)
我有更好的实现方法来观看用户的聊天状态。
if (packet instanceof Message) {
Message message = (Message) packet;
if (message.getBody() != null) {
processMessage(message);
} else {
//process chat states here
processChatState(message);
}
}
processChatState
的方法是
private void processChatState(Message message) {
ExtensionElement element = message.getExtension(ChatStateExtension.NAMESPACE);
if (element != null) {
switch (element.getElementName()) {
case "composing":
updateStatusOfUser("typing...");
break;
case "paused":
updateStatusOfUser("online");
break;
case "active":
updateStatusOfUser("online");
break;
case "inactive":
updateStatusOfUser("online");
break;
case "gone":
updateStatusOfUser("online");
break;
}
}
}