Office Hours — Today, November 17

Friday, November 14

Nov 17
8:50 AM
Mark M.
has entered the room
Mark M.
turned on guest access
9:40 AM
Craig D.
has entered the room
Mark M.
hello, Craig!
how can I help you today?
9:45 AM
Craig D.
Hey. I need to broadcast notifications at user-defined times. What is the best way to do that? I tried setting up an array of BraodCastReceivers, but only the last one fired.
Mark M.
do you mean that there are N times with one broadcast per time?
or there is one time with N broadcasts?
Craig D.
There are n broadcasts to go off at different times during the same day.
Mark M.
have one "manager" BroadcastReceiver that is triggered by AlarmManager
have that BroadcastReceiver send the N broadcasts
though I am rather confused why you're doing any of this
Craig D.
These are for notifications that the user defines like when to take your pills during the day
Mark M.
then what is a "broadcast" Notification?
don't you mean a Notification?
sans "broadcast"?
Craig D.
I thought that the notifications would be triggered by a broadcastreceiver
9:50 AM
Craig D.
Is that the right approach?
Mark M.
they *could* be, but there is no technical requirement that a Notification be raised by a BroadcastReceiver
so, to recap: at various times in the day, you want to raise various Notifications -- correct?
Craig D.
yes
Mark M.
OK
particularly given Android's move away from exact repeating alarms starting in Android 4.4, you probably have one BroadcastReceiver, triggered by one alarm in AlarmManager
that alarm would be scheduled using setExact() (API Level 19+) or set() (API Level 18 and below)
and it would be scheduled for the nearest time when you need to raise a Notification
Craig D.
So would I have one alarm for each notification time?
Mark M.
the BroadcastReceiver that is triggered by the alarm would raise the Notification for that event, plus call setExact() or set() for scheduling itself for the next event
no, you would have one alarm, period
at any point in time, you would have one outstanding alarm, for whenever the next Notification is supposed to be raised
Craig D.
OK. I understand now.
Mark M.
Android offers repeating alarms, which in theory would be good for at least some of your notifications (e.g., "take your pills at 8am each day"), but Android 4.4+ with targetSdkVersion of 19+ no longer allows exact repeating alarms
9:55 AM
Mark M.
instead, they are all inexact, and will go off sometime around when you ask, but not necessarily all that close
for periodic work that is not "in the face" of the user, inexactness is great, as it can consume less power
for something like this, you'll want exact alarms, and so I'm steering people to do that manually using setExact()/set()
Craig D.
OK. Thanks. I see that our chat is scrolling off the top of the screen. Is that an archive available after the chat?
Mark M.
I post the transcript shortly after the chat ends
though you should be able to scroll upwards to see everything since you signed into the chat
Craig D.
Thanks. I read the section in the book on AlarmManager. There are a lot of options (not your fault)!
Mark M.
yeah, they *really* want developers to move away from exact alarms, which made things more complicated starting with Android 4.4
and, now there's JobScheduler (which is also inexact and therefore not especially useful for your situation)
BTW, Version 6.2 of the book was released an hour or two ago
if you haven't done so, be sure to download your copy
Craig D.
Cool! I will definitely get it.
Thanks
10:00 AM
Mark M.
and that's a wrap for today's chat
the next chat is next Monday, at 7:30pm US Eastern
have a pleasant day!
Craig D.
thank you.
Craig D.
has left the room
Mark M.
turned off guest access

Friday, November 14

 

Office Hours

People in this transcript

  • Craig Doremus
  • Mark Murphy