Trying to keep an xmpp connection alive using a service in android
public class XMPPService extends Service {
XMPPTCPConnection connection;
String USERNAME;
String PASSWORD;
public XMPPService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
onHandleIntent();
return START_STICKY;
}
private void onHandleIntent() {
connection = XMPPClient.getConnection();
connection.addConnectionListener(
new AbstractConnectionListener() {
public void connectionClosed() {
Log.i("connection", "closed");
}
public void connectionClosedOnError(Exception e) {
Log.i("connection", "closed on error");
}
public void reconnectionFailed(Exception e) {
Log.i("reconnection", "failed");
}
public void reconnectionSuccessful() {
if (connection.isAuthenticated()) {
Log.i("isauthenticauted : ", String.valueOf(connection.isAuthenticated()));
Log.i("reconnection", "succesful");
} else {
try {
connection.login(USERNAME, PASSWORD);
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("reconnection", "succesful");
}
}
public void reconnectingIn(int seconds) {
Log.i("reconnectingIn", String.valueOf(seconds));
}
}
);
}
@Override
public void onCreate() {
Log.i("service", "created");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
My service restarts when I kill my app. Why is this happening? When I restart it gives me an exception NetworkOnMainThreadException at connection = XMPPClient.getConnection(); I don't want my service to restart when I kill my app.