gpt4 book ai didi

java - Smack API,ChatMessageListener 接口(interface)无法正常工作

转载 作者:行者123 更新时间:2023-11-29 03:00:17 25 4
gpt4 key购买 nike

我是 smack api 的初学者,我已经浪费了 1 天的时间来试验和理解它。请客气

下面的方法没有被调用:

@Override
public void processMessage(Chat chat, Message message) {
Log.d("processMessage234:", "Getting Message here");

}

下面是我的完整java代码:

public class Activity2 extends AppCompatActivity implements ChatManagerListener,ChatMessageListener,PacketListener {

MyXmpp2 myXmpp2;
Button isOnline;
AbstractXMPPConnection connection;
ChatManager chatManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
isOnline = (Button) findViewById(R.id.button6);
myXmpp2 = new MyXmpp2();


}

@Override
protected void onResume() {
super.onResume();


isOnline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
connection = myXmpp2.getConnection();
Log.d("isOnline:", myXmpp2.getConnection().isConnected() + "");

chatManager = ChatManager.getInstanceFor(connection);
Chat c = chatManager.createChat("baba@abc", new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
Log.d("processMessage000:", "Getting Message here");
}
});

try {
c.sendMessage("lol");
//This is working fine
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
}

});

}

@Override
public void chatCreated(Chat chat, boolean createdLocally) {

if (!createdLocally) {
chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
Log.d("processMessage:", "Getting Message here");
}
});
}
}

@Override
public void processMessage(Chat chat, Message message) {
Log.d("processMessage234:", "Getting Message here");
//This is not getting invoked

}

@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
Log.d("processPacket:", "Getting Packets here");
}

我可以在日志中读取发送的数据包和接收的数据包,如下所示:

D/SMACK: SENT (0): <message to='baba@abc ......
D/SMACK: SENT (0): <message from='baba@abc ......

但是我无法调用该方法

void processMessage(Chat chat, Message message){.....  }

任何建议都非常有帮助:)

最佳答案

感谢 Mack94 挽救了我的一天,我正在上传我的完整代码以帮助其他人

public class Activity2 extends AppCompatActivity {

MyXmpp2 myXmpp2;
Button isOnline;
AbstractXMPPConnection connection;
ChatManager chatManager;
ChatManagerListener chatManagerListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
isOnline = (Button) findViewById(R.id.button6);
myXmpp2 = new MyXmpp2();



}

@Override
protected void onResume() {
super.onResume();


isOnline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
connection = myXmpp2.getConnection();
Log.d("isOnline:", myXmpp2.getConnection().isConnected() + "");


chatManager = ChatManager.getInstanceFor(connection);
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");
if(message.getBody()!=null)
{
Log.d("Message::",message.getBody());

}
}
});
}
};

}

}

关于java - Smack API,ChatMessageListener 接口(interface)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35458334/

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