Office Hours — Today, January 7

Friday, January 3

Jan 7
7:20 PM
Mark M.
has entered the room
Mark M.
turned on guest access
8:15 PM
George S.
has entered the room
Mark M.
hello, George!
how can I help you today?
George S.
Hi Mark!
I have a question about the AlarmManager
Mark M.
go right ahead
George S.
I write an app, BostonBusMap, which downloads public transit live data every 15 seconds or so and displays the results to the user
I'm looking to add an alarm to the app to allow the user to turn off the screen for a short period
The data comes in the form of predictions, some number of minutes until the bus comes to a particular stop
It wouldn't be sufficient to just set a one time alarm because the data may change in the next few minutes
So I'd like to check every 30 seconds or so and then send a notification if the bus is arriving (or if data is lost or some other problem occurs)
8:20 PM
George S.
Is the best option AlarmManager for this scenario? Set a one time alarms 30 seconds into the future, then check and set again if necessary?
Mark M.
well, that depends
you mention the screen turning off
George S.
Or should I get a wake lock and have a service running for the time? Or something else?
Mark M.
what if the user simply leaves your app, via HOME or the recent-tasks list or something?
do you still want to be alerting the user (until a positive move from the user to tell your app to stop)?
George S.
Yes, I think that would be a common use case, if they were reading the news or something on their phone they would still want the alert
Mark M.
then, yes, AlarmManager is probably the best bet
prior to Android 4.4, you can use setRepeating()
Android 4.4+, you will need to use setExact() and reschedule it manually
you would then need to acquire a WakeLock to be able to do your check
whether you use WakefulBroadcastReceiver, my WakefulIntentService, or something else for that is up to you
George S.
Does that mean I would need to use setExact to support all phones? Or only if targetSdkVersion was for Android 4.4?
Mark M.
setExact() was only added in Android 4.4
so you cannot use it on earlier versions
8:25 PM
George S.
Do you forsee any significant battery problem for doing this for 10 minutes or so?
Is the drain for keeping awake significant compared to keeping the screen on?
Mark M.
it's not ideal, particularly since a lot of this will be done with mobile data versus WiFi
the CPU and the mobile radio chew up battery, though less than the screen
the mobile radio, in particular, chews up more power than you might expect, as it takes a while for it to "cool down"
now, will this be *bad* for just 10 minutes? probably not
however, you do need to have some plan in mind to ensure that it'll only be that long, even if the user forgets about you
checking every 30 seconds for 24 hours would be bad
George S.
right
Thanks for your help!
Mark M.
note that if your targetSdkVersion is lower than 19, setRepeating() will still be exact, even on Android 4.4+ devices
I had forgotten about that
so, in the short term, you may be able to just use setRepeating()
but you'll then have some additional work in the future when you do decide to raise your targetSdkVersion to 19 or higher
8:30 PM
Mark M.
and that's a wrap for today's chat
the transcript will be posted to http://commonsware.com/office-hours/ shortly
the next chat is Thursday at 10am Eastern
have a pleasant day!
George S.
has left the room
Mark M.
turned off guest access

Friday, January 3

 

Office Hours

People in this transcript

  • George Schneeloch
  • Mark Murphy