Skip to content Skip to sidebar Skip to footer

Android Service Stop When Application Is Reopened And Closed

i'm trying to understand how android services works. So i've created a simple application with an activity and a service, there is the code: MainActivity.xml:

Solution 1:

Have you read through https://developer.android.com/reference/android/app/Service.html ?

Note the following:

For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them.

From the START_STICKY documentation:

Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.

This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.


Post a Comment for "Android Service Stop When Application Is Reopened And Closed"