I have an app which connects to XMPP. Every 5 minutes I disconnect and reconnect (don't ask, boss wanted it...) - Up until now I've had an AsyncTask
to do this in the background and it's been running fine. Today, however, is a different story (bear in mind that the code hasn't changed for quite some time! And is SC'd via SVN, I've checked that no changes have happened since the last release).
Today it'll disconnect and start the connection after 5 minutes, but only after a reboot. Any subsequent attempts don't even bother to try.
The runnable that reconnects:
private final Runnable killConnection = new Runnable()
{
@Override
public void run()
{
synchronized(checkLock)
{
Log.d("BXC", "Killing connection and restarting");
// Manually disconnect and restart the connection every 5 minutes
destroyConnectionAndRestart();
new LoginTask().execute();
mHandler.postDelayed(this, mInterval5m);
}
}
};
And the AsynTask itself:
private class LoginTask extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params)
{
android.os.Debug.waitForDebugger();
String deviceUsername = Utility.getAndroidID(GaggleApplication.getInstance());
try
{
//bConnecting = true;
Log.d("BXC", "Beginning connection");
synchronized(connectLock)
{
// First ensure we've got a connection to work with first
if(Utility.hasActiveInternetConnection(getApplicationContext()) &&
((!bConnecting) && (!Globals.backgroundXmppConnectorRunning)))
{
String randomResource = Utility.randomString();
setupConnection();
if(xConnection != null)
{
xConnection.connect();
}
else
{
bConnecting = false;
Globals.backgroundXmppConnectorRunning = false;
setupConnection();
xConnection.connect();
}
/*
I know, I know... Don't specify a resource, but more often than not, OpenFire, doesn't
specify a unique resource, so at least this way, if we disconnect and then reconnect,
we're at least promised a resource that isn't being used already and leave OpenFire to
remove it's own idle connections, after X minutes.
*/
if(accountManager.supportsAccountCreation() && (!SystemProperties.getBoolean("accountCreated")))
{
Log.d("BXC", "Server supports registering custom user accounts");
accountManager.createAccount(deviceUsername, AppSettings.XMPP_KEYSTORE_PASSWORD);
SystemProperties.setBoolean("accountCreated", true);
}
xConnection.login(deviceUsername, AppSettings.XMPP_KEYSTORE_PASSWORD, randomResource);
ChatManager.getInstanceFor(xConnection).addChatListener(new ChatManagerListener(){
@Override
public void chatCreated(final Chat chat, boolean createdLocally)
{
if(!createdLocally)
{
// add chat listener //
chat.addMessageListener(new BackgroundMessageListener(BackgroundXmppConnector.this));
}
}
});
Presence p = new Presence(Presence.Type.subscribe);
p.setStatus("Out and About");
xConnection.sendPacket(p);
mBuilder.setSmallIcon(android.R.drawable.presence_online)
.setContentTitle("Connected")
.setContentText("Connected to XMPP server");
mNotificationManager.notify(mNotificationId, mBuilder.build());
Roster r = xConnection.getRoster();
r.setSubscriptionMode(SubscriptionMode.accept_all);
r.createEntry(AppSettings.BOT_NAME, "AbleBot", null);
r.addRosterListener(new RosterListener(){
@Override
public void entriesAdded(Collection<String> addresses)
{
for(String s : addresses)
{
Log.d("BXC", "Entries Added: " + s);
}
}
@Override
public void entriesDeleted(Collection<String> addresses)
{
for(String s : addresses)
{
Log.d("BXC", "Entries Deleted: " + s);
}
}
@Override
public void entriesUpdated(Collection<String> addresses)
{
for(String s : addresses)
{
Log.d("BXC", "Entries updated: " + s);
}
}
@Override
public void presenceChanged(Presence presence)
{
Log.d("BXC", "PresenceChanged: " + presence.getFrom());
}
});
}
else{
Log.i("BXC", "Already connected or in process of connecting. ");
}
}
}
catch(IllegalStateException ex)
{
Log.e("BXC", "IllegalStateException -->");
if(ex.getMessage().contains("Already logged in to server"))
{
Globals.backgroundXmppConnectorRunning = true;
}
else
{
Globals.backgroundXmppConnectorRunning = false;
Utility.writeExceptionToLog(getApplicationContext(), ex);
ex.printStackTrace();
}
}
catch(XMPPException ex)
{
Log.e("BXC", "XMPPException -->");
Globals.backgroundXmppConnectorRunning = false;
Utility.writeExceptionToLog(getApplicationContext(), ex);
ex.printStackTrace();
}
catch(NullPointerException ex)
{
Log.e("BXC", "NullPointerException -->");
Globals.backgroundXmppConnectorRunning = false;
Utility.writeExceptionToLog(getApplicationContext(), ex);
ex.printStackTrace();
}
catch(Exception ex)
{
Log.e("BXC", "Exception -->");
Globals.backgroundXmppConnectorRunning = false;
Utility.writeToLog(getApplicationContext(), ex.toString());
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void ignored)
{
if(xConnection != null)
{
if(xConnection.isConnected() && (!xConnection.isSocketClosed()))
{
Log.i("BXC", "Logged in to XMPP Server");
Globals.backgroundXmppConnectorRunning = true;
ConnectionState cs = new ConnectionState(true);
GaggleApplication.getBusInstance().post(cs);
cs = null;
// SEND A WHOAMI TO UPDATE DEVICE FRIENDLY NAME //
WhoAmI wai = new WhoAmI();
Intent intent = new Intent(GaggleApplication.getInstance(), BackgroundXmppConnector.class);
intent.putExtra("MESSAGEDATA", new Gson().toJson(
Utility.makeTransaction(GaggleApplication.getInstance(), MessageType.Type.WHOAMI, wai)));
sendMessage(intent);
Log.i("MAIN", "Sent WHOAMI transaction");
mHandler.postDelayed(checkConnection, mInterval1m);
}
else
{
Log.e("BXC", "Unable to log into XMPP Server.");
Globals.backgroundXmppConnectorRunning = false;
destroyConnectionAndRestart();
}
}
else
{
Log.e("BXC", "Xmpp Connection object is null");
Globals.backgroundXmppConnectorRunning = false;
ConnectionState cs = new ConnectionState(false);
GaggleApplication.getBusInstance().post(cs);
cs = null;
}
}
}
I've tried debugging with the help of this SO post: How do I use the Eclipse debugger in an AsyncTask when developing for Android? - And as described above, any subsequent attempts don't fire the doInBackground
- There is nothing of any relevance in LogCat
either, I'm a little lost.
If anyone has experienced this before, any help is greatly appreciated?