创建 Xmpp 连接并在不重新创建的情况下传递实例的最佳方法
Best way to create an Xmpp connection and passing the Instance without Re-creation
我有 class 称为 XmppChatManager
,它具有所有必要的方法,如 initializingConnection()
和 sendOneToOneMessage()
,其他 class 需要使用此 class 需要做这样的事情:
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;
}
}
我有 class 称为 XmppChatManager
,它具有所有必要的方法,如 initializingConnection()
和 sendOneToOneMessage()
,其他 class 需要使用此 class 需要做这样的事情:
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;
}
}