powered by Jive Software

How to correctly disconnect from aSmack

I have running aSmack 8.4.0.0 on android. Connection is running in a service. I need to completely disconnect from aSmack and stop the service,

I am doing :

mConnection.removeConnectionListener(mConnectionListener);

mConnection.disconnect();

mSmackAndroid.onDestroy();

mConnection = null;

And setting to null all used Managers/listeners/receivers

But after starting the service again I have very strange situation.

aSmack is connected, reads the roster, and in debug mode I see that it receives messages from server, but not delivering it to app. And also when I try to sent a message I have ConnectionException.

But when I completely kill the app, then aSmack is correctly connected to server.

What you wrote in your service to connect XMPP ?

mSmackAndroid = SmackAndroid.init(context);

mConnectionChangeListeners = new ArrayList();

mXmppRoster = XmppRoster.getInstance(context);

mXmppRoster.registerListener(this);

mXmppChatManager = XmppChatManager.getInstance(context, this);

mXmppMUCManager = XmppMUCManager.getInstance(context, this);

mXmppFileTransferManager = XmppFileTransferManager.getInstance(context, this);

mXmppChatManager.registerListener(this);

mXmppMUCManager.registerListener(this);

mXmppFileTransferManager.registerListener(this);

mMessageStorage = new MessageStorage();

IntentFilter intentFilter = new IntentFilter(Constants.ACTION_STORAGE_NEW_MESSAGE);

mContext.registerReceiver(mStorageUpdateReceiver, intentFilter);

ConnectionConfiguration config = new ConnectionConfiguration(XMPP_HOST, 5222);

SASLAuthentication.supportSASLMechanism(“PLAIN”, 0);

config.setSendPresence(false);

config.setReconnectionAllowed(false);

config.setDebuggerEnabled(true);

config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);

mConnection = new XMPPTCPConnection(config);

mConnection.getRoster();

try {

mConnection.connect();

mPingManager = PingManager.getInstanceFor(mConnection);

mPingManager.registerPingFailedListener(pingFailedListener);

if (mConnection.isConnected()) {

mConnection.login(mLogin, mPassword);

onConnectionEstablished();

}

} catch (Exception e) {

Log.w(ID, “XMPP connection failed”, e);

if (e instanceof XMPPException) {

Log.w(ID, "XMPP connection failed because of stream error: " + e.getMessage());

}

}

Is there necessary a special sequence of comands when disconnecting, or to wait until aSmack get disconnected or anything ?

Reconnection is not allowed as I am handlig this manually

It seems you have started your service as STICKY and this is the reason its connect to XMPP server again when you completely kill your app from task manager .

You dont have to connect XMPP server every time when user come to the app.

Check if connection is not connected then only try to connect in your running service .

connection.isConnected()

{

Do you code to connect

}

else

nothing