Reputation: 23607
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!
Upvotes: 4
Views: 2700
Reputation: 1007359
I have a service I run in the background connected to another thread that counts down a timer.
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.
I seem to have fixed the issue (and see no real battery use) using startForeground.
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 use AlarmManager
, and you will not need startForeground()
.
Now I read about wakelocks, are there any best practices on when to use one or the other?
WakeLock
keeps the CPU powered on (and possibly other things, like the screen). It has little to do with startForeground()
. If you use AlarmManager
, depending on the work that you are doing, you may need a WakeLock
to ensure the device stays awake while you do your perodic wo
Upvotes: 2