【问题标题】:Best way to create an Xmpp connection and passing the Instance without Re-creation创建 Xmpp 连接并在不重新创建的情况下传递实例的最佳方法
【发布时间】:2018-09-28 01:14:02
【问题描述】:

我有一个名为 XmppChatManager 的类,它具有所有必要的方法,例如 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();
                }
            });

这几乎是我开始所需要的,我遇到的问题是当我尝试从 chatClass 调用 sendOneToOneMessage() 方法时,我必须获取创建的连接的实例,因为Smack Error 会发生如果我在连接已经启动时尝试重新连接!所以我需要通过创建一个名为getInstance() 的方法来提供该启动连接的实例我想这可能很简单,但我已经尝试过,但我没有得到它。有没有更好的处理方法?

【问题讨论】:

    标签: java android android-studio xmpp xmppframework


    【解决方案1】:

    让你的 xmppchatmanager 成为单例,例如

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

    【讨论】:

      猜你喜欢
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      相关资源
      最近更新 更多