Since I was unable to run a service every day at 0:00, I decided to run it every 50 sec (by AlarmManager). I read that running a service every 1 sec kills the battery. I tried out my program with 1 sec refresh rate as well as with 50 secs refresh rate. For this I used various applications to measure the memory usage and the cpu usage. Here are the results:
Memory usage:
Android Booster
1 sec: 5.1 - 7.5 MB
50 sec: 5.5-7.2MB
Memory Usage/Memory Manager
1 sec: 24.5MB
50 sec: 23MB
Usage Timelines
1 sec: 24MB
50 sec: 22.7 MB
Watchdog Task Manager
1 sec: 14.6MB
50 sec: 12-14MB
Android Assistant:
1 sec: 4MB
50 sec: 7MB
CPU usage:
Usage Timelines
1 sec: 2%
50 sec: 0%
Watchdog Task Manager
1 sec: 1.3%-1.9%
50 sec: 0.1%
Android Assistant:
1 sec: 2%-3%
50 sec: 0.4%-2%
All in all we can say that the memory usage has not changed but the cpu usage has changed spectacularly. As all used applications show different data, I cannot be sure how much memory/cpu my app uses but this little investigation showed me that running a service every 1 sec really is a pain in the开发者_开发知识库 ass for the cpu. Or isn't it? What is your opinion about it?
The basic rule is that the less you do the less you impact the battery. In other words, everything you do reduces the battery. If you are updating every minute, changing that to every 2 minutes will typically halve the drain on the battery.
But beyond that, there is no single number to give that is the "right" amount. You want to run as little as possible, doing as little work as possible.
Taking a look at the numbers you do is good, it gives you some idea of what your impact is. However they also aren't the whole story -- for example on some common CPUs simply holding a partial wake lock will cause battery drain.
A good way to look at this is that we want the battery on our devices to last at least N days, where N is at least one and really we'd like it to be more than one. If the device is not being used -- its screen is off -- it is still doing stuff in the background draining the battery: the phone radio is running, probably some things are syncing new e-mail and such on to the device, etc.
If the device wasn't doing that work, it would last for days and days and days. You can see this yourself: turn on airplane mode, make sure there aren't dumb apps doing stuff in the background still, and see how long your battery lasts. It can easily be a week.
So each little thing you do in the background isn't itself an issue. It is the aggregate of that work, in a world where we want to be running for days on a charge. And the longer the basic device can run on a charge, the more each of your little pieces of work reduces the battery life, because there is more time for you to run and drain it.
Ultimately, scheduling some work to happen every minute or more will reduce the user's battery life. How much that is depends on a lot of things: the power behavior of the hardware, how long the user can typically run on a charge, how long you run each of those times, the kinds of work you are doing.
One thing I would definitely recommend is just testing this app yourself, on your own device. Start with a baseline: leave your device sitting without use, and see how long it lasts. Be sure to not have other third party apps on it that could impact its life (starting with Android 3.1 you can just force stop all those apps to be sure they won't run). Consider turning off sync of e-mail and such to remove that noise from the picture.
Now put your app on it running at the amount you think it should, and see how much it reduces your battery life. It will be some, it is just a question of how much.
Also when you do these things, you want to focus on the longest time you can last on a battery on a reasonably running system and how your app impacts that. The longer a user's battery lasts, the more your app will impact it, and these thus are the ones who are more likely to be unhappy and give bad reviews on Market.
精彩评论