我有一个名为 XmppChatManager
的类(class)其中具有所有必要的方法,例如 initializingConnection()
和sendOneToOneMessage()
,其他需要使用这个类的类需要做这样的事情:
block 引用>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 calledgetInstance()
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/