Can a background service exist without its main application?
Asked Answered
A

3

13

Lets examine the next scenario:

  1. I created a new android app, with an application class, activity and a background service.
  2. I have some static variable in the application class, lets say it's an int i = 0;
  3. I start the activity, and start the service from the activitie's onCreate(), the service gets the START_STICKY flag.
  4. All that this service does is using TimerTask to write the current second to the variable in the application class. 5.I exit the activity
  5. After a while, Android will kill the service, and the application (lets say that the device is low on resources), and restart the service because of the START_STICKY flag.

Now I have to questions:

  1. Is the situation when OS kill's the application, but doesn't kill the service?
  2. When the service is restarted by the system, will the application restart as well? If yes - which context will it have, and if not, how could it be, that there is a service running without his application?

Thanks, sorry about my terrible English...

Adhern answered 7/3, 2013 at 9:48 Comment(0)
D
8

1. Is there a situation when OS kill's the application, but doesn't kill the service?

tl;dr: Yes, this is possible. However, the Service must be started in it's own process.

Explaination:

It's important to realize that the Android OS kills processes when it's running low on memory, not individual components, such as Activities or Services (see this answer).

Given the above statement, it is clear that a Service can exist independent of the Application only if they are contained in separate processes. Otherwise, they will be destroyed together when their process is destroyed.

Now consider the case of the Service and Application existing on separate processes. In Android, processes are destroyed in low memory situations from lowest to highest priority. The priority order is: Empty < Background < Service < Visible < Foreground (see here). Therefore, it's possible that your Application will be destroyed while your Service stays alive (e.g. if your Application is in the background) and it's also possible that your Service will be destroyed while your Application stays alive (Application is in the foreground).

You can declare any component (Activity, Service, ContentProvider, etc.) of an application to run in it's own process by defining the android:process attribute in the components manifest tag.

From the official documentation of Processes:

By default, all components of the same application run in the same process and most applications should not change this. However, [...] the manifest entry for each type of component element — Activity, Service, Receiver, and Provider — supports an android:process attribute that can specify a process in which that component should run. You can set this attribute so that each component runs in its own process or so that some components share a process while others do not. [...] The Application element also supports an android:process attribute, to set a default value that applies to all components.


2. When the service is restarted by the system, will the application restart as well?

This is related to the answer in question 1.

If the Service exists in the same process as the Application then they will both be destroyed and restarted together.

If the Service exists in a separate process as the Application then they are completely separate processes, and therefore will be destroyed and restarted independent of each other as the Android OS deems appropriate.

Demetrademetre answered 10/7, 2015 at 20:48 Comment(0)
B
1

Is the situation when OS will kill the application, but won't kill the service?

yes, there is such situation, generally services will kill after activity, because they demand last resources from the System read Docs here

When the service is restarted by the system, will the application restart as well?

am not sure about that, but I think no because service can live without the application reference

Belkisbelknap answered 7/3, 2013 at 9:53 Comment(0)
M
1

The answer by Tomer Mor is not correct.

When your Application dies - your Services die too.

Your Services will be created only after your Application. This you can observe yourself by logging their onCreate().

Madelaine answered 13/3, 2013 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.