Question

I have a android service wich needs to be running even if activity is closed!

If screen get OFF the service does not need to do anything.

If screen get ON the service start their work!

So my question is, its better remove all handler callbacks (keeping service) or stop the entire service and when screen get ON start service again?

Thanks!

Was it helpful?

Solution

in principal, it's always would be better to stop all running services if they are not needed.

from battery consumption aspects - running Service itself don't have any significant affect on the battery consumption, but as long as your Service still alive - your entire process is still alive - what means that your app still consume RAM amount, and considered "running". let's say that the fact itself that your app is running don't have also such impact on the battery life.

but (and there's a big but)

what's do matters, and could have huge impact on battery life is what you are doing within your Service.

from your question I can assume that you are executing some code in some time interval, and thus - you are consuming at each execution CPU time, and maybe even worse - performing network request / performing IO file operations and so on. performing such operations frequently could drain very fast the battery , and you should avoid them as much as possible.

so, if you performing one or more from the operations I mentioned - it's defiantly would be better to not perform them when the screen is off, and not only ignore their callbacks...

I advice you also to watch the entire video - http://www.youtube.com/watch?v=OUemfrKe65c

it's would make you understand the whole picture...

OTHER TIPS

If you stop you service and re-launch service on every screen on/off event, then system will perform more work than if you keep it active and unregister handlers. System will need to recreate service, if it has threads or caches, to recreate those too etc. Moreover, if you have a foreground service, then user will see services icon appearing and disappearing constantly.

I would keep it started. Just remove all handlers, that it doesn't consume any resources.

The whole point of a Service is that it can run in background, even when your Activities are not displayed or when the screen is off. If the Service is not doing anything, (in your case, when the screen is off) than it's not consuming any battery.

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top