Lets examine the next scenario:
static
variable in the application class, lets say it's an int i = 0;activity
, and start the service
from the activitie's onCreate()
, the service gets the START_STICKY
flag.service
does is using TimerTask to write the current second to the variable in the application class.
5.I exit the activitySTART_STICKY
flag.Now I have to questions:
Thanks, sorry about my terrible English...
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.
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()) } .... }
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.
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.
tl;dr: Yes, this is possible. However, the Service
must be started in it's own process.
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
, andProvider
— supports anandroid: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. [...] TheApplication
element also supports an android:process attribute, to set a default value that applies to all components.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With