0

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.

  • 1
    Show us your code, service, activity, where you have start the serivce. – romtsn Nov 01 '14 at 10:50
  • @Romadja code blocks added. –  Nov 01 '14 at 11:30
  • So, what is your problem now? If your returning START_REDELIVER_INTENT it means that you are ready for service destroying, and can reestablish it, so it shouldn't cause any problems. – romtsn Nov 02 '14 at 06:51
  • Where do you call `stopService()`? – Flow Aug 14 '15 at 11:54
  • @Flow I do not call this at all. –  Aug 14 '15 at 12:34
  • Then why do you expect your service from being stopped then? – Flow Aug 14 '15 at 12:55
  • I don't expect it to stop, that's the whole point. The question is about why is the service stopping and restarting when the app quits, while the point in a service is it being separate from the app and able to run in the background until it finishes it's work, regardless of the app running or not. –  Aug 15 '15 at 14:06

1 Answers1

0

If method startService() is used to start the service in other places,it will restart itself even though the activity binded to it was destroyed.So make sure you start the service only by bindService() method.

imooncat
  • 21
  • 4
  • But I need the service to run after the activity unbinds from it and to my understanding, if I start the service using bindService only, it will stop the moment my activity unbinds from it. Am I correct? –  Nov 01 '14 at 11:31
  • Actually, not. If you unbind from the service it just means, that your activity is not communicating with your service, but the service should be alive. – romtsn Nov 01 '14 at 11:41
  • So should I drop the startService, and only use bindService? What if I want the service to start before I bind to it? (eg. make sure its running when the app starts, and only bind to it in a specific activity) –  Nov 01 '14 at 11:44
  • @Romadja [this answer](http://stackoverflow.com/a/3514742/2213590) states that services started by binding will die when the activity unbinds from them –  Nov 01 '14 at 11:59
  • Yes, if you will not call startService(), just bindService(), in that case the service will die when you unbind it from the activity. But as I can see you use startService() it means that when you unbind service from the activity it won't die – romtsn Nov 01 '14 at 12:02
  • You can avoid bindService using, just use startService() and communicate with your application by sending broadcasts, using LocalBroadcastManager and BroadcastReceiver in activities where you want to get data from the service. – romtsn Nov 01 '14 at 12:05
  • Exactly, but the answer suggests I should remove the startService() and only use bindService to solve the restarting problem, but I can't do this since my service would stop when the activity dies. Regarding your suggestion - that would be less efficent and very uncomfortable to work with. Also, see my edit to the question. –  Nov 01 '14 at 12:05
  • See the edit to the question. It seems like this problem has nothing to do with binding to the service or not - the service is killed when you exit the app, and the restarts itself since it has START_REDELIVER_INTENT. –  Nov 01 '14 at 12:43
  • I think this is a solution:explicitly call stopService when you destroy the activity.Set a SharedPreference,get its value at the service's onCreate() method and use stopSelf() when the value is true. – imooncat Nov 09 '14 at 12:05