6

I have a small chat application. When it starts it binds to a service. This service has a tcp/ip connection with the server.

When the application is not in foreground my service creates a status notification. So far so good.

When the application is destroyed for example using task manager on "onDestroy" method I call unbind. Now the service is killed. So my question is how I make a service to stay alive even there is no clients bind with it.

Googe doc: " The service will remain running as long as the connection is established (whether or not the client retains a reference on the service's IBinder)."

Thanl you.

Catalin
  • 752
  • 1
  • 16
  • 32

3 Answers3

9

You can make your service a foreground service, which will display an icon in the notification bar so that the user can see your service is running. Otherwise, you cannot keep your service running. By the way, you should be aware that an everlasting service is an Android antipattern, the system should be able to kill off your app when memory is low, and apps haven't used it in a while. If you are doing something that legitimately needs to live "forever" you should do so with a foreground service.

Kristopher Micinski
  • 7,572
  • 3
  • 29
  • 34
  • 3
    It is true that using a foreground service will do the trick. BUT applications like what's up or yahoo messenger don't use a foreground service, I never saw a notification on status bar. So how this apps work ? Thanks – Catalin Aug 18 '12 at 22:09
  • 2
    They will eventually die, or use an alarm manager to restart them, or use cloud to device messenger, let of repeat, your service will be killed after long enough, there's nothing you can do, no special flag, nothing, this is just how android works – Kristopher Micinski Aug 18 '12 at 23:36
  • What is the best practice for using alarm manager. For example I could set alarm manager to restart the service from 5 to 5 minutes. This way even my service is killed it's restarted immediately. But I'm not sure what android thinks about it :) – Catalin Aug 19 '12 at 09:28
  • 2
    Generally this is considered very poor programming. You should let the user decide how much your app is used. If the user interacts with the service (does something that causes it to be woken up) then you get brought to the top of the stack anyway and you essentially "restart." Also, it's unlikely you will die in "5 to 10" minutes, probably more like 5 to 10 *hours*. An AlarmManager is "okay" for the right purposes, but make sure you *let the user decide to quit* the app. The "correct" solution for this is cloud to device messaging, as I said. – Kristopher Micinski Aug 19 '12 at 19:39
5

read this:

http://developer.android.com/guide/components/services.html#Foreground

android developer
  • 114,585
  • 152
  • 739
  • 1,270
2

If you want to keep your service alive even when your binder activity is destroyed, you can start your service in Activity first then call bindService:

startService(serviceIntent);

bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
Amir
  • 203
  • 1
  • 8
  • Can you explain what are de difference and how is this possible? Thanks – Catalin Aug 23 '12 at 08:44
  • You can get the better explanation here: [link](http://stackoverflow.com/a/3514742/1545562) Hope it helps. – Amir Aug 23 '12 at 16:18
  • Yes it makes sense. So if I start it with startSerive, theoretically my service would stay alive untill i kill it or system kills it on low memory. So if system kills it how would you sugest to handle it and somehow restart the service ? Thanks – Catalin Aug 23 '12 at 18:56