A service can be killed by android?

Asked

Viewed 35 times

1

I know that depending on the time that one Activity be in a state of onPause() and if the device needs to release memory it will kill this Activity. I wonder if the same happens with the service that runs in the background? When the Activity is killed the service will also be killed?

I have a service that does a localization update using the Google Maps API from time to time, but I believe that after a long time on, and the user go using other applications he is killed.

  • How to detail how this service runs in the background so we can help in the best way?

  • 1

    Sure, I’ll edit the question.

1 answer

1

Contrary to what the documentation seems to suggest, to free memory the system does not kill Activity It is the entire process of your application. Usually the services are killed together because they belong to the same process (although it is possible to define that they run in separate processes, although uncommon).

It is possible reduce the chances of the service being killed, putting it to run on "foreground" (foreground). But it is not very desirable as it makes the user aware of the running service, through a notification in the notification bar.

Ideally depending on the situation would set that the service should be restarted even in case the process dies. This is done through flags like for example START_STICKY:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //**Seu código**
    return START_STICKY;
}
  • I have already defined this, but I believe he dies... because I stop receiving the updates, after a long time..

  • There’s also START_NOT_STICKY and START_REDELIVER_INTENT, did you ever try them? Also, it might be good for you to include your service code in the question, so we can better understand what is happening.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.