Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a background service exist without its main application?

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...

like image 549
Slava Kamzen Avatar asked Mar 07 '13 09:03

Slava Kamzen


People also ask

What are background services?

Background. A background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.

How do I run background services continuously?

1: You have to invoke the service's startForeground() method within 5 seconds after starting the service. To do this, you can call startForeground() in onCreate() method of service. public class AppService extends Service { .... @Override public void onCreate() { startForeground(9999, Notification()) } .... }

Does a service run in the background?

For Android Developers, a Service is a component that runs on the background to perform long-running tasks. A Background Service is a service that runs only when the app is running so it'll get terminated when the app is terminated.

How do I start a background service?

Start android studio and right-click the package name in the android studio left project panel. Click the menu item New —> Service —> Service. Give the android background service a name by input its name in the next New Android Component window Class Name input box, check both the Exported and Enabled checkbox.


1 Answers

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.

like image 54
bcorso Avatar answered Oct 13 '22 19:10

bcorso