4

Can I run a background service Indefinitely ? Will android kill my service if i run it for indefinitely ? How Facebook android application keep running in background for a long time ?? please help me to know about it .

Sunil_raut27
  • 41
  • 1
  • 5

3 Answers3

4

You can use for that these features:

1) Auto-restart service after reboot (Start intent after Reboot)

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

These features both helps to leave your service started all time as possible.

Community
  • 1
  • 1
Dimmerg
  • 2,113
  • 1
  • 13
  • 14
  • Thanks Dimmerg it helped me up to certain extent.but my application is getting killed by android due to insufficient memory .how can I overcome this problem. – Sunil_raut27 Jun 17 '13 at 08:37
  • Do your service takes a lot of memory? What type of operations do you in the service? – Dimmerg Jun 17 '13 at 09:01
  • In my service I am storing frames from an ip camera in my wifi network . and it is also reducing the speed of my device . – Sunil_raut27 Jun 17 '13 at 09:39
  • It's heavy task for mobile device. Try these: 1) Start separate thread in your service for long and "heavy" operations 2) Mark service as foreground: http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification) – Dimmerg Jun 17 '13 at 09:51
3

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

The first thing you can try is to use foreground services, a foreground service is a high priority services that won't be killed unless is completely necessary (note that these services increase battery consumption). You can find an example here using compatibility with older devices, otherwise you only need to call startForeground inside your service.

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

  • START_STICKY will restart your service with an empty intent so everytime you have to recover the data you need to run your service.

  • START_REDELIVER_INTENT in this case your service will be restarted with the last intent information (it could be that you run your service several times with different information when you want different behaviour).

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

Hope it helps :)

zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
  • Thanks zozelfelfo it helped me a lot.I have a question for you if I am running multiple threads and multiple services in my application will my performance of my device will come down??help me regarding this topic – Sunil_raut27 Jun 17 '13 at 08:43
  • It depens on what the threads/services are doing and the speed of the device, but most likely there would be a noticeable speed drop. – Boardy Jun 17 '13 at 09:23
  • I have installed facebook app. in my device its running fine and not even getting killed by android and not reducing my device performance .how can i achieve this optimization in my application .help me – Sunil_raut27 Jun 17 '13 at 09:49
  • First of all, all android devices have limited amount of threads running at the same time, but I would say that yes, your performance will probably come down due to de amount of load in every thread. With services we can say the same because services are running in the UI thread, a lot of services may reduce your application smoothness – zozelfelfo Jun 17 '13 at 11:16
0

There's no way to absolutely protect a Service from being killed.

If you return START_STICKY from onStartCommand(), then even if Service is killed, android will try its best to start it again, when resources are free.

S.D.
  • 29,290
  • 3
  • 79
  • 130