Admittedly I am just kind of hacking here so I would like some knowledge.
I have a service I run in the background connected to another thread that counts down a timer. I was having problems with the count down dying after a while and presumed it was due to garbage collection of the service. I seem to have fixed the issue (and see no real battery use) using startForeground. Now I read about wakelocks, are there any best practices on when to use one or the other?
Thanks!
Please use
AlarmManager, so your service only needs to be in memory when it is actually doing work, not just watching the clock tick. One of the biggest user complaints about Android is all these background things that developers create that clog up their phones.The point behind
startForeground()is to indicate that your service is continuously delivering value to the user, such that the user will notice if the service is reclaimed due to hanging around too long or low memory conditions. Sitting and watching the clock tick is not “continuously delivering value to the user”. Please useAlarmManager, and you will not needstartForeground().WakeLockkeeps the CPU powered on (and possibly other things, like the screen). It has little to do withstartForeground(). If you useAlarmManager, depending on the work that you are doing, you may need aWakeLockto ensure the device stays awake while you do your perodic wo