Android-XMPP断开连接

时间:2015-04-10 05:28:05

标签: android xmpp asmack disconnect

我正在为我的应用程序使用aSmack和Openfire。和Spark进行测试。

我正在编写我的连接代码和广播接收器,它在STICKY服务中监听网络状态。

当我关闭无线网络时,互联网会关闭,但Spark仍会显示为“可用”,3-4分钟后我的状态变为“不可用”。

我尝试了xmppConnection.disconnect()和Presence.Type.unavailable。但他们都没有为我工作。

断开客户端的互联网连接后,如何立即断开XMPP服务器/发送状态“不可用”?

这是我的代码:

// Connect XMPP Server
public void connectXMPPServer() {

    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {

            // Create a connection
            ConnectionConfiguration connConfig = new ConnectionConfiguration(
                    HOST, PORT, SERVICE);
            connConfig.setReconnectionAllowed(true);

            xmppConnection = new XMPPConnection(connConfig);

            try {

                xmppConnection.connect();
                Log.i("XMPPChatDemoActivity", "Connected to "
                        + xmppConnection.getHost());
            } catch (XMPPException ex) {

                Log.e("XMPPChatDemoActivity", "Failed to connect to "
                        + xmppConnection.getHost());
                Log.e("XMPPChatDemoActivity", ex.toString());
            }

            try {

                if (xmppConnection.isConnected()) {

                    xmppConnection.login(USERNAME, PASSWORD);
                    Log.i("XMPPChatDemoActivity", "Logged in as "
                            + xmppConnection.getUser());

                    Presence presence = new Presence(
                            Presence.Type.available);
                    xmppConnection.sendPacket(presence);
                }
            } catch (XMPPException ex) {

                Log.e("XMPPChatDemoActivity", "Failed to log in as "
                        + USERNAME);
                Log.e("XMPPChatDemoActivity", ex.toString());
            }
        }
    });
    t.start();
}

// Broadcast receiver; listens to network state
public BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        try {

            if (isNetworkOn()) {

                Log.i("BackgroundService-BroadcastReceiver",
                        "Network is on");
                connectXMPPServer();
            } else if (!isNetworkOn()) {

                Log.i("BackgroundService-BroadcastReceiver",
                        "Network is off");
                Presence presence = new Presence(Presence.Type.unavailable);
                xmppConnection.sendPacket(presence);
                xmppConnection.disconnect();
            }
        } catch (Exception e) {

            Log.e("BackgroundService-networkStateReceiver", e.toString());
        }
    }
};

// Returns network state
public boolean isNetworkOn() {

    ConnectivityManager connMngr = (ConnectivityManager) getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connMngr.getActiveNetworkInfo();
    return (netInfo != null && netInfo.isConnected());
}

1 个答案:

答案 0 :(得分:0)

您必须添加一个ConnectionListener,一旦检测到连接丢弃,就会调用它。您可以在UI中更新您的状态。

发送状态是为了让其他人了解您的状态,如果您没有连接,您显然无法做到这一点。服务器会在检测到断开连接时为您执行此操作。