gpt4 book ai didi

java - 创建 Xmpp 连接并传递实例而无需重新创建的最佳方法

转载 作者:太空宇宙 更新时间:2023-11-04 10:32:04 27 4
gpt4 key购买 nike

我有一个名为 XmppChatManager 的类(class)其中具有所有必要的方法,例如 initializingConnection()sendOneToOneMessage() ,其他需要使用这个类的类需要做这样的事情:

XmppChatManager xmppchatmanager = new XmppChatManager(this, userId, password);

runOnUiThread(new Runnable() {
public void run() {
Thread thread = new Thread(){
public void run(){
try {
xmppchatmanager.initializingConnection();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
}
}
};
thread.start();
}
});

This is pretty much all I need to get started, the issue i'm having is when I try calling the sendOneToOneMessage() method from the chatClass, I have to get the instance of the created connection because Smack Error will occur if I try reconnecting when the connection is already initiated! So I need to make available the instance of that initiated connection by creating a method called getInstance() I guess this might be simple but I have tried but I'm not getting it. Any one with better way of handling such?

最佳答案

让你的xmppchatmanager成为单例,例如

public class XmppChatManager{
static class Loader{
static XmppChatManager sInstance = new XmppChatManaget();
}

public static XmppChatManager getInstance(){
return Loader.sInstance;
}
}

关于java - 创建 Xmpp 连接并传递实例而无需重新创建的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49888791/

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