Is it possible to start my service indefinitely?

Is it possible to start a background service endlessly? Will an android kill my service if I run it indefinitely? How does the android android app constantly work in the background? please help me find out about this.

+4
source share
3 answers

You can use for these functions:

1) Auto restart service after reboot ( Start washing after reboot )

2) Sticky maintenance mode ( http://developer.android.com/reference/android/app/Service.html#START_STICKY )

These features help you leave your service as quickly as possible.

+4
source

Android will kill your service if it runs out of memory, but you can do a couple of things to recover from it.

The first thing you can try is to use the front-end services, the front-end service is high-priority services that will not be killed if it is not completely necessary (note that these services increase battery consumption). You can find it here using compatibility with older devices, otherwise you only need to call startForeground inside your service.

Another thing you can do is use some flags in your service to restart it when it is killed by the OS. You can use 2 different flags (depending on what behavior you want to reproduce).

  • START_STICKY will restart your service with no intention, so each time you need to restore the data necessary to start your service.

  • START_REDELIVER_INTENT, in this case, your service will be restarted with the latest information about intentions (you may perform your service several times with different information when you want a different behavior).

Flags can be used as follows:

@Override public int onStartCommand(Intent intent, int flags, int startId) { //Do your service work return START_STICKY; //or return START_REDELIVER_INTENT; } 

Depending on what you need, use one or the other.

Hope this helps :)

+3
source

There is no way to completely protect Service from killing.

If you return START_STICKY from onStartCommand() , then even if the service is killed, the android will try to do everything possible to start it again when the resources are free.

0
source

Source: https://habr.com/ru/post/1486540/


All Articles