Android的asmack:stateChange从来没有所谓的 [英] Android asmack: stateChange never called
问题描述
我建立使用asmack XMPP客户聊天应用程序。聊天工作正常,我已经实现了 ChatStateListener
,但 stateChanged
方法不会被调用。目前获得作曲状态我解析的XML消息。下面是合成,文字,活跃。
I am building a chat application using asmack xmpp client. Chat works fine and I have implemented ChatStateListener
but stateChanged
method never gets called. Currently to get composing status I am parsing the message xml. Below is the message format for composing,text, active.
<message id='5ec7d' to='admin@testserver123.net' from='praveenraj@testserver123.net/682e3641' type='chat'><composing xmlns="http://jabber.org/protocol/chatstates" /></message>
<message id='9a93f22' to='admin@testserver123.net' from='praveenraj@testserver123.net/682e3641' type='chat'><body>hi</body><active xmlns="http://jabber.org/protocol/chatstates" /></message>
<message to='admin@testserver123.net' from='praveenraj@testserver123.net/682e3641' type='chat'><active xmlns="http://jabber.org/protocol/chatstates" /></message>
但解析XML来获得作曲状态不是一个好主意。有人可以帮助我理解为什么 stateChanged
永远不会被调用。
推荐答案
我工作的一个项目现在使用aSmack为好,这是我解决了这个问题,希望这可以帮助你。
我假设你已经创建的实例<一个href=\"https://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smackx/chatstates/ChatStateManager.html\"相对=nofollow> ChatStateManager 的,如:
I am working on a project right now using aSmack as well, this is how I solved the problem, hopefully it helps you out. I assume that you have created an instance of ChatStateManager such as:
ChatStateManager chatStateManager = ChatStateManager.getInstance(connection);
然后发送撰写的状态,其中的连接是您当前的XMPP连接和 currentChat 是在聊天您对当前会话创建的
Then to send the composing state, where connection is your current xmpp connection and currentChat is the Chat you created for the current conversation
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(connection != null){
try {
chatStateManager.setCurrentState(ChatState.composing, currentChat);
} catch (Exception e) {
e.printStackTrace();
}
}
}
其他客户端会送你一个<一个href=\"https://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smack/packet/Packet.html\"相对=nofollow>与不同的状态,在下面的情况下,数据包是A 作曲状态
<message id='16vn2-83' to='jabberusername@ip-address' from='jabberusername@ip-address' type='chat'><thread>781de2f5-8883-4b16-a3b2-3bf7aff1efe9</thread><composing xmlns="http://jabber.org/protocol/chatstates" /></message>
现在这是它得到乐趣,(回答你的问题)。我抓住每一个进入的数据包,并将其发送到BroadcatsReceiver通知我吧。请注意,如果输入的数据包有一个空的身体,这意味着它不是一个实际的消息文本,但A ChatState 的消息
Now this is where it gets fun, (to answer your question). I grab every incoming Packet and send it to a BroadcatsReceiver to notify me of it. Note that if the incoming packet has a null body that means it's not an actual message with text but a ChatState message.
if (packet.getBody() == null) {
Intent i = new Intent();
i.setAction(Constants.ACTION_TYPING_LISTENER);
i.putExtra(Constants.ACTION_EXTRA_WHO_TYPING, getSimpleUsername(packet.getFrom()));
if (isIncomingComposingMessage(msg.toXML().toString())) {
i.putExtra(Constants.ACTION_EXTRA_MESSAGESTATE, ChatState.composing.toString());
} else {
i.putExtra(Constants.ACTION_EXTRA_MESSAGESTATE, ChatState.paused.toString());
}
sendBroadcast(i);
}
和
public boolean isIncomingComposingMessage(String xmlMessage) {
if (xmlMessage.indexOf(ChatState.composing.toString()) == -1) {
return false;
} else {
return true;
}
}
我知道这可能只是一个解决办法,如果有人读这更好的答案,请张贴,所以我们都可以借鉴一下。
I know this might be just a "workaround" and if somebody reading this has a better answer please post it so we can all learn from it.
感谢你,我希望它能帮助。
Thank you and I hope it helps.
这篇关于Android的asmack:stateChange从来没有所谓的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!