5

In an effort to keep my services alive, can I have more than one service in foreground?

I am starting two services at once, and I don't want any of them to be destroyed. So, after reading for hours, I found the solution is to return START_STICKY and use startForeground(...) on my service. But, I have two services, so can I have them both be in the foreground, and both never be destroyed?

Trinimon
  • 13,839
  • 9
  • 44
  • 60
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • 1
    I don't understand your question? Do you want two `Services` to keep alive or do you want to keep you `Activities` alive? Returning start sticky in a `Service` has no influence on the lifecycle of any `Activity`. – Xaver Kapeller Feb 03 '16 at 04:59
  • 1
    @RuchirBaronia May be you should use only one service and create two thread inside that service to serve your purpose. – Dhaval Patel Feb 03 '16 at 05:00
  • @XaverKapeller I don't care about my activity. I just want to have the long running service in the background, even when the phone screen is asleep and when the app is closed. – Ruchir Baronia Feb 03 '16 at 05:02
  • 1
    And just a spoiler up front: What you are trying to do is not possible. Nothing can keep something alive indefinitely on Android. Sure returning `START_STICKY` increases the chances of your `Service` starting alive but there are still a million things that could happen which would cause your `Service` to be killed anyway - sticky or not. – Xaver Kapeller Feb 03 '16 at 05:03
  • @XaverKapeller I know it is possible for the service to get destroyed, but the chances are highly unlikely, right? If not, is there anything else I can do in order to keep the service running? – Ruchir Baronia Feb 03 '16 at 05:03
  • @MD Are you sure? Can I just have a notification in both? – Ruchir Baronia Feb 03 '16 at 05:05
  • @DhavalPatel I want both the tasks to execute at the same time. How can I do it in threads? – Ruchir Baronia Feb 03 '16 at 05:05
  • You can bind one service and do this but may be chance when your app is destroyed then your all foreground services also destroy. – M D Feb 03 '16 at 05:09
  • @RuchirBaronia You can start your both task in onStartCommand method of your service. – Dhaval Patel Feb 03 '16 at 05:10
  • @MD I thought binding will make my service disappear if my activity is destroyed? I want it to be persistent. – Ruchir Baronia Feb 03 '16 at 05:10
  • 2
    @RuchirBaronia How high the chances are depends on the specific device. They can be higher or lower but they are never negligible. The important part is: it is never guaranteed that anything stays alive. Android was not designed this way and the policies deciding when something is killed have consistently become more and more aggressive which each new Android release. As they should of course. You cannot relie on something staying alive because it is fundamentally the wrong approach on a mobile device. – Xaver Kapeller Feb 03 '16 at 05:10
  • @XaverKapeller Okay, is there anything else I can do to try and increase the chances? – Ruchir Baronia Feb 03 '16 at 05:19
  • @RuchirBaronia No, use `START_STICKY`, keep your memory footprint low and try to minimize how much work you do in the `Service`. If you want your app to work reliably you have to change your approach in a way so you don't need your `Service` to keep running. Also be aware that it is very easy to destroy the battery life of older devices (not as easy on newer devices but still very easy) if you have a `Service` running in the background which constantly performs some work. – Xaver Kapeller Feb 03 '16 at 05:28
  • And if you really want to do this (which you shouldn't) be sure to never use `WakeLock`s. Only bad things can come from `WakeLock`s unless you really know what you are doing. – Xaver Kapeller Feb 03 '16 at 05:28
  • @XaverKapeller Okay, thanks for the advice. So is it possible to have two services in foreground? – Ruchir Baronia Feb 03 '16 at 05:30
  • Sure, why not? I just don't understand why you would want to do that. And you shouldn't do that, all that does is make things more complicated for you. Whatever those two `Services` do can be combined into one `Service` - and that would be much better. – Xaver Kapeller Feb 03 '16 at 05:39
  • @XaverKapeller Okay, so maybe I can combine the two services in one by putting their tasks in different threads, and running the two threads at the same time, right? – Ruchir Baronia Feb 03 '16 at 05:39
  • @XaverKapeller Also, I would use a service for this, not an IntentService, right? – Ruchir Baronia Feb 03 '16 at 05:40
  • @XaverKapeller Because I am not binding anything, I can just ignore the `onBind()` method, right? Thanks – Ruchir Baronia Feb 03 '16 at 06:12
  • Yes, you should just return `null`. – Xaver Kapeller Feb 03 '16 at 06:15
  • @XaverKapeller When and how would I use that method? – Ruchir Baronia Feb 03 '16 at 06:20
  • Which one? `onBind()`? – Xaver Kapeller Feb 03 '16 at 06:21
  • @RuchirBaronia It is a lifecycle method of the `Service`. You would implement it if you would want to bind to the `Service`. – Xaver Kapeller Feb 03 '16 at 06:39

1 Answers1

6

There are several aspects to consider.

  1. First of all: if you have two services in foreground, you have to have two notifications as well. This is considered as a security feature (check for instance this post). Having two icons in the status bar for one app might be quite confusing to the user and is IMHO reason enough to drop this idea.
  2. As stated in the comments: even if starting in foreground, a service might be destroyed anyway. Just keep this in mind. However, in my experience this happens rather rarely, almost never - except with ...
  3. ... devices that are shipped with pre-installed start managers or energy savers (many Huawei devices for instance). If your app is not configured appropriately your services will be killed several seconds/minutes after the screen turns off - or it will not automatically be started after reboot.

So, as always it depends on what exact you want to achieve ;) But to me it looks like as if you could simply implement two threads in one sticky service - I can't any see disadvantages here.

If your app gets inactivated by any kind of start manager, crash, or force close, your users might have to start the app again (see this post including app states).

Hope this helps!

Community
  • 1
  • 1
Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • What could I do to "configure appropriately" as you stated in your third point – Ruchir Baronia Feb 03 '16 at 16:20
  • 1
    There is nothing you can do about this. If manufacturers include apps that stop yours from working (correctly) the only thing you can do is to tell users how they have to configure these tools. Sorry to say ... :( – Trinimon Feb 04 '16 at 16:01
  • I disagree. Some scenarios would prefer 2 notifications over 1. Imagine an app that was designed to monitor multiple things and could independently be stopped / started, there's no way to allow this in one notification. An example is a timer app, multiple timers etc.. – behelit May 18 '18 at 00:37
  • @behelit: as you said: _"some scenarious ..."_ - yes, there are some for sure. However, op's question was related to _"In an effort to keep my services alive ...."_ and I think it is no good idea to use multiple sticky services **in general**, just in order to handle multiple long runnning background tasks. But again, as I wrote: _"it depends on what exact you want to achieve"_. – Trinimon May 18 '18 at 10:40