I have an android service which is designed to always run in the background, similar to what WhatsApp's MessageService does. When the app starts, it makes sure the service is indeed running. Later on, one of the activites binds to the service to use some of it's methods in addition to what the service already does, and when the activity destroys - it unbinds from the service. So far so good, this seems to work.
But when I close my app (normally, not force stop), the service seems to restart itself over a minute or so and then continues to work normally. But when I look at WhatsApp's service, I see that this does not happen - after you close the app the service continues to run normally and does not restart itself.
Any hints on what's causing this and how to solve it?
EDIT Code, as requested:
The relevant part of the service:
public static boolean isRunning = false;
...
...
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
if (!isRunning) {
isRunning = true;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
connect(intent.getIntExtra(Constants.ID, -1),
intent.getStringExtra(Constants.PASSWORD));
}
});
thread.start();
}
return START_REDELIVER_INTENT;
......
}
Starting the service when the app starts
if (!ChatService.isRunning) {
Intent chatService = new Intent(this, ChatService.class);
chatService.putExtra(Constants.ID, LocalManager.getID());
chatService.putExtra(Constants.PASSWORD, LocalManager.getPassword());
startService(chatService);
}
Binding to the service in one of the activites
void bindService() {
bindService(new Intent(Chats.this, ChatService.class), mConnection,
Context.BIND_AUTO_CREATE);
mIsBound = true;
}
SECOND EDIT Turns out it has nothing to do with binding to service, the same behaviour occurs even when I don't bind to the service (the service restarts when the app closes). Running the service on a seperate process didn't solve this either.