Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I gracefully handle an Android service being killed by OS?

Tags:

android

I have a service which is required to run for long periods of time in the background. A foreground service is not suitable due to the notification. It needs to run quietly in the background doing it's polling. It does this already and behaves as it should. This is not my problem, this is just some background.

When the service is killed by the user and onDestroy() is called, the data is saved to the db - No problem there. Also when the phone is turned off I have a broadcast receiever which listens for screenoff/on. Again no problem saving there.

The Problem is when the OS kills the service due to low memory. The service is killed and I don't believe onDestroy() is called. If it is, my data is not stored. So if the service is killed and has been running for a while, all the information it has gathered is lost.

So after all that, my question is: How should I gracefully clean up my resources and save my data? Is there a way I can run some code to save my data before the system kills my service? I have considered making my service save to the database at intervals of a few minutes to reduce the amount of data loss. I am not sure how good this is performance wise. The queries would likely only be inserting up to 30 rows in a period of say 5 minutes. Likely less.

I am a relatively new to Android development and have tried my best to look up this problem before coming here to ask a question. Apologies if I have overlooked something.

Thanks!

like image 935
Matt R Avatar asked Jul 25 '13 10:07

Matt R


People also ask

How can we prevent a service from being killed by OS?

If you really needed Android not to kill your service, your best bet would be to make it a System App, or make your service return START_STICKY in your onStartCommand() method. This way, if your service gets killed, then it will be queued to restart automatically.

How do you handle running service when an app is killed by swiping in Android?

You can't handle swipe, because system just removes your process from memory without calling any callback. I have checked, that before user calls "recent apps" screen, onPause() will be always called. So you need to save all data in onPause method without checking isFinishing().

How do you run the service even after killing the application in Android?

If your Service is started by your app then actually your service is running on main process. so when app is killed service will also be stopped. So what you can do is, send broadcast from onTaskRemoved method of your service as follows: Intent intent = new Intent("com.

Which method is called when app is killed Android?

When Android decides to kill our app, our activities will call onDestroy method.


1 Answers

If your service is killed by os, non of your code could be executed on this event, so you need to save your data once it changed.

If fact, a background service will restart if it return Service.START_STICKY_COMPATIBILITY in onStartCommand(), it's the default choice and you don't need to worry about it. Just take a quick initialization in your service and save your result as soon as possible.

like image 73
faylon Avatar answered Sep 21 '22 09:09

faylon