Jan 7 | 7:20 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Jan 7 | 8:15 PM |
George S. | has entered the room |
Mark M. |
hello, George!
|
Mark M. |
how can I help you today?
|
George S. |
Hi Mark!
|
George S. |
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
|
George S. |
I'm looking to add an alarm to the app to allow the user to turn off the screen for a short period
|
George S. |
The data comes in the form of predictions, some number of minutes until the bus comes to a particular stop
|
George S. |
It wouldn't be sufficient to just set a one time alarm because the data may change in the next few minutes
|
George S. |
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)
|
Jan 7 | 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
|
Mark M. |
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?
|
Mark M. |
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
|
Mark M. |
prior to Android 4.4, you can use setRepeating()
|
Mark M. |
Android 4.4+, you will need to use setExact() and reschedule it manually
|
Mark M. |
you would then need to acquire a WakeLock to be able to do your check
|
Mark M. |
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
|
Mark M. |
so you cannot use it on earlier versions
|
Jan 7 | 8:25 PM |
George S. |
Do you forsee any significant battery problem for doing this for 10 minutes or so?
|
George S. |
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
|
Mark M. |
the CPU and the mobile radio chew up battery, though less than the screen
|
Mark M. |
the mobile radio, in particular, chews up more power than you might expect, as it takes a while for it to "cool down"
|
Mark M. |
now, will this be *bad* for just 10 minutes? probably not
|
Mark M. |
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
|
Mark M. |
checking every 30 seconds for 24 hours would be bad
|
George S. |
right
|
George S. |
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
|
Mark M. |
I had forgotten about that
|
Mark M. |
so, in the short term, you may be able to just use setRepeating()
|
Mark M. |
but you'll then have some additional work in the future when you do decide to raise your targetSdkVersion to 19 or higher
|
Jan 7 | 8:30 PM |
Mark M. |
and that's a wrap for today's chat
|
Mark M. |
the transcript will be posted to http://commonsware.com/office-hours/ shortly
|
Mark M. |
the next chat is Thursday at 10am Eastern
|
Mark M. |
have a pleasant day!
|
George S. | has left the room |
Mark M. | turned off guest access |