问题描述
我想在xmpp的组(多用户)聊天中发送撰写事件,我正在使用asmack库,我已经完成了一对一聊天的相同功能.
I want send composing event in Group (Multiuser) chat in xmpp, I am using asmack library, I have done same functionality with One to One chat.
我正在使用以下代码:
mMessageEventManager = new MessageEventManager(XMPPConnectApplication.getInstance().getXmppConnection());
mMessageEventManager.addMessageEventNotificationListener(new MessageEventNotificationListener() {
@Override
public void offlineNotification(String arg0, String arg1) {
}
@Override
public void displayedNotification(String arg0, String arg1) {
}
@Override
public void deliveredNotification(String arg0, String arg1) {
}
@Override
public void composingNotification(String from, String to) {
Log.e("Receiver-composingNotification",from + " is started typing......"+to);
}
@Override
public void cancelledNotification(String from, String to) {
Log.e("Receiver-cancelledNotification",from + " is stopped typing......"+to);
}
});
如果您有相同的想法,请告诉我.
Please let me know if you have any idea for the same.
任何帮助将不胜感激.
Any help will be appreciated.
推荐答案
是的,我对此有所了解,并且已经在1周前完成了.
Yes, I have idea about it and I have done just before 1 week.
我已经使用 MessageEventManager 来管理聊天状态.
private MessageEventManager mMessageEventManager;
为聊天状态接收监听器添加此方法:
private void chatStateRecognizer(){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
mMessageEventManager = new MessageEventManager(mXmppConnection);
mMessageEventManager.addMessageEventNotificationListener(new MessageEventNotificationListener() {
@Override
public void offlineNotification(String arg0, String arg1) {
}
@Override
public void displayedNotification(String arg0, String arg1) {
}
@Override
public void deliveredNotification(String from, String arg1) {
}
@Override
public void composingNotification(String from, String to) {
Log.i("Receiver:Compose state",from + " is started typing......"+to);
}
@Override
public void cancelledNotification(String from, String to) {
Log.i("Receiver:Stop state",from + " is stopped typing......"+to);
}
});
}
});
thread.start();
}
使用 GroupInfoModel.java 创建一个模型类名称:
Create one Model class name with GroupInfoModel.java:
public class GroupInfoModel implements Comparable<GroupInfoModel>, Serializable{
private static final long serialVersionUID = 1L;
private String memberId = "", memberName = "";
private boolean isAdmin;
public String getMemberId() {
return memberId;
}
public void setMemberId(String memberId) {
this.memberId = memberId;
}
public String getMemberName() {
return memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
public boolean isAdmin() {
return isAdmin;
}
public void setAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
}
@Override
public int compareTo(GroupInfoModel another) {
return getMemberName().compareTo(another.getMemberName());
}
}
现在获取 GroupInfoModel.java 类的 ArrayList :
private ArrayList<GroupInfoModel> groupDetailsList = new ArrayList<GroupInfoModel>();
private boolean isComposingStarted;
在活动/片段的 onCreate()上:
groupDetailsList.clear();
ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(mXmppConnection);
DiscoverItems items = discoManager.discoverItems(mRoomId);
for (Iterator<Item> it = items.getItems(); it.hasNext();) {
DiscoverItems.Item item = (DiscoverItems.Item) it.next();
String occupant = item.getEntityID();
occupant = occupant.split("/")[1];
GroupInfoModel groupInfoModel = new GroupInfoModel();
groupInfoModel.setAdmin(false);
groupInfoModel.setMemberId(occupant+"@"+mServiceNameHere);
groupInfoModel.setMemberName(occupant);
groupDetailsList.add(groupInfoModel);
}
现在在撰写邮件(聊天视图)屏幕的 EditText 上添加 TextWatcher :
Now add TextWatcher on your EditText of Compose Message (Chat view) screen:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.toString().length()==1&&!isComposingStarted){
isComposingStarted = true;
if(chatType.equals("OneToOneChat")){
mMessageEventManager.sendComposingNotification(myJabberId, friendJabberId);
}else if(chatType.equals("GroupChat")){
for (int i = 0; i < groupDetailsList.size(); i++) {
if(!groupDetailsList.get(i).getMemberId().contains(myJabberId)){
mMessageEventManager.sendComposingNotification(groupDetailsList.get(i).getMemberId(), roomId);
}
}
}
}else if(s.toString().length()==0){
isComposingStarted = false;
if(chatType.equals("OneToOneChat")){
mMessageEventManager.sendCancelledNotification(myJabberId, friendJabberId);
}else if(chatType.equals("GroupChat")){
for (int i = 0; i < groupDetailsList.size(); i++) {
if(!groupDetailsList.get(i).getMemberId().contains(myJabberId)){
mMessageEventManager.sendCancelledNotification(groupDetailsList.get(i).getMemberId(), roomId);
}
}
}
}
}
我强烈推荐,在 Application class 中使用上述代码,您可以根据需要修改方法.
I strongly recommended that use above code in Application class, you can modify methods as your requirements.
完成.
这篇关于如何在多用户聊天/群聊中发送撰写/正在键入(聊天状态)事件在XMPP Android中进行一对一聊天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!