Sri
Sri

Reputation: 85

Android: Service need to run continuously without killed by the OS

In my app, there is a service checking for incoming audio calls (through internet). I am facing that the service has been killed by the OS after sometime being idle, but the notification icon appears like the service is running and in phone settings its displaying that the service is currently running. I found some apps on google play where service is not killed even on low memory.

I added START_STICKY to restart it when it is stopped. I also made it to run as foreground.

I want my service not to be killed. May I know the procedure for it. (If the service is killed, it should reflect in the notification and in phone settings).

Upvotes: 0

Views: 475

Answers (1)

Ev0xFrost
Ev0xFrost

Reputation: 56

Having read this question, immediately want to refer you to a blog post, which really helped me to understand how android achieves multitasking, and where services fit into this context.

"http://android-developers.blogspot.com/2010/04/multitasking-android-way.html"

But, to summarize, there is no way to truely prevent being killed, and, when android decides it needs to reclaim memory, "it will do so brutally, simply killing the process" that this service resides in.

So, two things - first, you have to develop your application so that in the event of this service being killed, it will recover. There is no way to truely guarantee this won't happen.

Secondly, you should minimize the likelihood of this occuring by responding to memory events - documented here:

http://developer.android.com/reference/android/content/ComponentCallbacks2.html#onTrimMemory(int)

Basically, you can develop your application to be notified when Android is about to start killing processes, and when you receive that notification, respond by freeing up memory.

Upvotes: 1

Related Questions