简体   繁体   English

在整个应用程序中保持XMPP连接(使用smack)

[英]Keep XMPP connection(using smack) alive throughout application

我正在使用XMPP连接(使用smack)在android应用程序中进行聊天。我已经与openfire建立了连接,我也可以发送和接收消息。但问题是当我进入XMPPClient.java活动然后它做了所以我不能得到任何消息,直到不进入那个活动。那么怎样才能在开始时建立连接,然后在其他活动中重用.Code在这2个链接ConnectionSettings文件和我们可以聊天的聊天屏幕中。在此链接中,评论行也是我的问题所以请同时查看该评论。

Create global XMPPConnection object and Use below funciton and store in global XMPPConnection object and use that connection object everywhere. 创建全局XMPPConnection对象并使用下面的函数并存储在全局XMPPConnection对象中,并在任何地方使用该连接对象。 This is a sample gtalk example. 这是一个示例gtalk示例。

    public XMPPConnection login() throws XMPPException {

         ConnectionConfiguration config = new
         ConnectionConfiguration("talk.google.com",5222,"gmail.com");   
         config.setSecurityMode(SecurityMode.required);
         config.setTruststoreType("BKS");
         config.setTruststorePath("/system/etc/security/cacerts.bks");
         XMPPConnection connection = new XMPPConnection(config);        
        connection.connect();
        connection.login(username, password);
        Presence presence = new Presence(Presence.Type.available);
        presence.setMode(Presence.Mode.available);  
        connection.sendPacket(presence);
        try {
            Thread.sleep(3000);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
     return connection;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM