- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,我正在尝试在其他人开始写作时获取输入状态,即使它正在返回一 strip 有状态的空消息作为消息,但它来自 proccessMessage 方法。它不应该在 stateChanged 中返回吗?我还没有启用任何 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>
最佳答案
这是一个笼统的答案,可能还有更好的答案,但这肯定有效,它可能包含您已经知道的内容,但我想它不会有什么坏处。
基本假设 - composing
消息是正文为 null 的消息,整个消息的 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 或 empathy 作为发送方)。
编辑 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) { }
}
接下来,在处理您的聊天的 Activity 中,添加一个新的匿名接口(interface)并使用您的状态更改消息发送方法调用实现它的回调,这是神奇的 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);
}
}
希望这对您有所帮助。
关于android - 键入状态 Smack 4.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30852692/
我使用 Smack API 编写我的 Google talk Client 。现在我需要处理这个添加请求。 我将 SubscriptionMode 设置为手动,现在我必须为存在数据包注册一个监听器,但
我正在尝试从应用程序服务器(TOMCAT)创建多用户聊天组,我已经在servlet中创建了一个XMPP机器人。 根据我的要求,我必须从应用程序服务器创建一个多用户聊天组(在应用程序服务器上使用 xmp
我正在将我的 android 应用程序从 asmack-android 库移动到 Smack 4.1.4。我在 Smack 的 asmack 版本中有一些 PacketExtensions,它使用 P
我正在使用 Smack API 开发多用户游戏客户端。我的 smack 应用程序遇到问题:我可以向服务器发送消息和请求,但我的应用程序无法从中读取响应。 Exception in thread "Sm
经过数小时的调试并试图找出为什么使用 aSmack 无法进行文件传输,而正常的消息传递却可以,我终于设法将其归结为这一点。 当我按照 Smack 文档中给出的方法获取用户的名册列表时,Openfire
我是 gcm 新手,我尝试使用 Smack API 连接到 GCM 的云连接服务器(XMPP)。一开始还好我的代码: uid = "123456789"; apiKey = "A***********
相关问题: Initialization exception "NoClassDefFoundError: javax.naming.directory.InitialDirContext" when
如何订阅以收听 SMACK 中的用户可用性状态变化? 要获取用户的可用性状态,我使用以下命令: XMPPConnection.getRoster().getPresence(name).isAvail
我知道之前有人问过这个问题,但 mschonaker here 只给出了部分答复.在我的网站上,用户可以将人添加到他们的花名册中,然后好友必须接受并最终连接。第一人称(用户a)使用著名的 roster
我正在为 openfire 聊天客户端创建一个 java fx 应用程序。我正在使用 smack 4.1 rc1 连接到服务器。我能够连接到服务器,向其他人发送存在信息,并向其他用户发送消息。但是我无
我正在为 openfire 聊天客户端创建一个 java fx 应用程序。我正在使用 smack 4.1 rc1 连接到服务器。我能够连接到服务器,向其他人发送存在信息,并向其他用户发送消息。但是我无
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在邀请离线用户加入群组,如下所示, myChattingRoom.invite("user7@10.10.1.105", "请加入我的聊天室"); 由于邀请消息默认没有正文,因此 openfire
我一直在努力通过 Smack 和 Openfire 服务器与 XMPP 聊天。 我的问题如下: 每当一个用户向另一个用户发送消息时,该消息就会被另一个用户正确接收。但是第一条消息的发件人不会显示任何回
我在 Java 的 Smack 消息传递库中建立适当的聊天时遇到问题。如果聊天是在本地开始的,它工作得很好,但如果聊天是从另一个客户端开始的,那么会创建两个不同的聊天线程,而不仅仅是需要的那个。我发送
我正在使用下面的代码向任何用户发送好友请求,它工作正常,但我无法从花名册列表中找到已连接的用户。 现在我想知道如何在花名册列表中识别新请求的 friend 。 Roster roster = XMPP
我正在使用 smack 和 openfire 在 android 中创建聊天应用程序。对于消息状态,我在其他地方发送和显示的消息没有问题客户(仔细检查)。我将向发件人发送一条简单的 json 消息,如
嗨, friend 们,我正在使用从以下网址下载的 Smack3.2.1.jar http://www.igniterealtime.org/downloads/index.jsp 我在我的构建路径中
我正在尝试为 asmack 数据包编写一个监听器。最终发生的事情是监听器有时只是没有获取数据包并对其进行处理。以下是部分代码: try { XMPPMethods.getCon
我有以下代码来监听聊天室的邀请: Connection.DEBUG_ENABLED = true; XMPPConnection connection = new XMPPConnec
我是一名优秀的程序员,十分优秀!