One way to do this would be through a "fuzzy" flag or value when setting
up an alarm in AlarmManager. Instead of a service saying "wake me up
every 5 minutes", it would say "wake me up every five-ish minutes" or
"wake me up in 5 minutes, but feel free to wait 3 more minutes if
needed". Depending on what's going on with the device, the OS could then
elect to delay certain alarms.
What we want is for AlarmManager to not raise alarms based on current
conditions. Once we fire the alarm, we're starting up the service again
(possibly starting a whole new process), and that will chew up some CPU
time.
> 2) The user has been playing a game for 30 minutes. In this situation
> maybe the email client reverts to checking email every 10 minutes, or
> some other user supplied setting. Or perhaps doesn't check at all
> depending on how the service author wants to write the service.
Here's one possible solution:
-- Add onBusyCPU() and onNormalCPU() methods that services could
override. Those might not be called simply for a scrolling list, but in
cases where a "game mode" activity is launched. The purpose would be to
allow services to quickly tailor their AlarmManager usage (e.g., cutting
back on alarms in onBusyCPU()) *before* the game gets underway.
-- Call onBusyCPU() when a "game mode" activity is launched for all
distinct services scheduled to be invoked via an alarm in the next hour.
That will require starting up each of those services (since they
probably aren't presently running). This will delay the start of the
game by a bit, depending on how many such services there are, but then
the game should run more smoothly.
-- Call onNormalCPU() after the "game mode" activity exits, for all
services for whom we called onBusyCPU().
> Certainly some effort would need to be in place to help encourage
> service authors to completely ignore this.
I think you're missing a "not" in that sentence. ;-)
What would be useful is if there were a way for AlarmManager to log all
alarms and have a screen, perhaps off of Settings, for the user to
review what's all going on in the background while they're playing the
game. Right now, there's no great way for users to have any idea who the
culprit is.
> Adding examples to the SDK and documentation.
That's the carrot. Users being able to positively identify who's causing
them grief, and giving appropriate feedback to problematic apps, is the
stick.
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy
Android App Developer Books: http://commonsware.com/books.html
JBQ
--
Jean-Baptiste M. "JBQ" Queru
Android Engineer, Google.
Questions sent directly to me that have no reason for being private
will likely get ignored or forwarded to a public forum with no further
warning.
Looks like it is setInexactRepeating().
I'm a little nervous about the phase-aligned alarms possibly
exacerbating the foreground app problem. If the device is asleep,
phase-aligning the alarms is great. If the device is not asleep (e.g.,
playing a game), you may want to avoid phase-aligning, so the device
doesn't all of a sudden slow down at the top of the hour, for example.
This might be handled under the covers -- it's tough to tell from the API.
Thanks!
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy
_The Busy Coder's Guide to Android Development_ Version 2.0 Available!