kob*_*der 1 android xmpp smack asmack
嘿我正在尝试在另一个人开始写入时获得输入状态,即使它返回一条状态为消息的空消息,但它来自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);
}
});
Run Code Online (Sandbox Code Playgroud)
日志:
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>
Run Code Online (Sandbox Code Playgroud)
这是一个普遍的答案,可能有更好的答案,但这肯定有效,它可能包含你已经知道的东西,但我想它不会受到伤害.
基本假设 - 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();
}
}
});
Run Code Online (Sandbox Code Playgroud)
编辑:
您的日志消息显示您收到了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) { }
}
Run Code Online (Sandbox Code Playgroud)
接下来,在处理聊天的活动中,添加一个新的匿名接口,并使用状态更改消息发送方法调用实现它的回调,这部分是神奇的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);
}
});
Run Code Online (Sandbox Code Playgroud)
最后但并非最不重要的是,在您的连接处理程序类中,您应该具有以下内容:
//.... 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);
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.