gpt4 book ai didi

java - 如何在不使用 createChat for android XMPP chat smack api 的情况下接收消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:02:11 32 4
gpt4 key购买 nike

您好,我对在 Service 中实现 chatManagerListener 接口(interface)的逻辑感到困惑。以下是我的服务代码:

public class MyService3 extends Service {
ChatManager chatManager;
ChatManagerListener chatManagerListener;
AbstractXMPPConnection abstractXMPPConnection;
MyXmpp2 myXmpp2;

public MyService3() {
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Myservice3:","Started");

abstractXMPPConnection = myXmpp2.getConnection();
abstractXMPPConnection.addConnectionListener(new ConnectionListener() {
@Override
public void connected(XMPPConnection connection) {
Log.d("XMPPConnection:","connected");
}

@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
Log.d("XMPPConnection:","authenticated");
//Once authenticated start listening for messages






}

@Override
public void connectionClosed() {
Log.d("XMPPConnection:","connectionClosed");
}

@Override
public void connectionClosedOnError(Exception e) {
Log.d("XMPPConnection:","connectionClosedOnError");
}

@Override
public void reconnectionSuccessful() {
Log.d("XMPPConnection:","reconnectionSuccessful");

}

@Override
public void reconnectingIn(int seconds) {
Log.d("XMPPConnection:","reconnectingIn");

}

@Override
public void reconnectionFailed(Exception e) {
Log.d("XMPPConnection:","reconnectionFailed");

}
});



Log.d("isOnline:", myXmpp2.getConnection().isConnected() + "");
chatManager = ChatManager.getInstanceFor(abstractXMPPConnection);
chatManager.addChatListener(chatManagerListener);
chatManagerListener = new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean createdLocally) {

chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {

Log.d("Hello::","World");
//NOT WORKNIG
if(message.getBody()!=null)
{
Log.d("Message::",message.getBody());
}
}
});
}
};





return super.onStartCommand(intent, flags, startId);
}



}

每当发送一个数据包时,我都会收到以下异常。我不知道它为什么会出现

Exception in packet listener java.lang.NullPointerException: Attempt to invoke interface method 'void org.jivesoftware.smack.chat.ChatManagerListener.chatCreated(org.jivesoftware.smack.chat.Chat, boolean)' on a null object reference
at org.jivesoftware.smack.chat.ChatManager.createChat(ChatManager.java:255)
at org.jivesoftware.smack.chat.ChatManager.createChat(ChatManager.java:287)

简单来说,我想知道如何在服务中实现 ChatMessage 监听器。请客气

最佳答案

成功连接和验证后,您需要创建聊天

一旦你得到了ChatManager的实例。为了包传输你需要createchat 与peer/group check this link用于创建聊天的方法。

 chatManager = ChatManager.getInstanceFor(abstractXMPPConnection);

newChat = chatmanager.createChat(userid, chatManagerListener);

一旦你得到Chat例如,您可以在 chatmanagerListner

上发送包裹和检索

newChat 您可以 sendMessage

To get Package (message, chat)

如果您的连接/身份验证过程成功完成,您可以尝试以下代码

final Chat newChat = ChatManager.getInstanceFor(xmppConn).createChat(userJid, new MessageListener() {
@Override
public void processMessage(final Chat arg0, final Message arg1) {
LOG.info("Sent message: " + arg1.getBody());
}
});
try {
final Message message = new Message();
message.setFrom(chatProperties.getDomain());
message.setTo(userJid);
message.setType(Type.normal);
message.setBody(text);
message.setSubject("");
newChat.sendMessage(message);
xmppConn.disconnect();
} catch (final Exception e) {
LOG.error("Error while sending message to " + userName + ": ", e);
}

更新

您可以尝试使用 PacketListener .

XMPPConnection 的 addPacketListener 方法 check this link for details .

使用 PacketFilter 将 PacketListener 添加到 XMPPConnection|输入消息

但是在添加 packetlistner 之前,如果已经在 xmppconnection 中添加了任何实例,则删除。

检查下面的代码

private PacketListener packetListener = new PacketListener() {
@Override
public void processPacket(Packet packet) {
if (packet instanceof Message) {
Message message = (Message) packet;
String chatMessage = message.getBody();
}
}
};

private void regiSterPackateListner() {
PacketTypeFilter filter = new PacketTypeFilter(Message.class);
try {
if (packetListener != null) {
//Avoid adding multiple packetlistner
abstractXMPPConnection.removePacketListener(packetListener);
}
abstractXMPPConnection.addPacketListener(packetListener, filter);
} catch (Exception e) {
e.printStackTrace();
}
}

关于java - 如何在不使用 createChat for android XMPP chat smack api 的情况下接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35477336/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com