Nov 17 | 8:50 AM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Nov 17 | 9:40 AM |
Craig D. | has entered the room |
Mark M. |
hello, Craig!
|
Mark M. |
how can I help you today?
|
Nov 17 | 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?
|
Mark M. |
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
|
Mark M. |
have that BroadcastReceiver send the N broadcasts
|
Mark M. |
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?
|
Mark M. |
don't you mean a Notification?
|
Mark M. |
sans "broadcast"?
|
Craig D. |
I thought that the notifications would be triggered by a broadcastreceiver
|
Nov 17 | 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
|
Mark M. |
so, to recap: at various times in the day, you want to raise various Notifications -- correct?
|
Craig D. |
yes
|
Mark M. |
OK
|
Mark M. |
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
|
Mark M. |
that alarm would be scheduled using setExact() (API Level 19+) or set() (API Level 18 and below)
|
Mark M. |
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
|
Mark M. |
no, you would have one alarm, period
|
Mark M. |
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
|
Nov 17 | 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
|
Mark M. |
for periodic work that is not "in the face" of the user, inexactness is great, as it can consume less power
|
Mark M. |
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. | |
Mark M. |
I post the transcript shortly after the chat ends
|
Mark M. |
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
|
Mark M. |
and, now there's JobScheduler (which is also inexact and therefore not especially useful for your situation)
|
Mark M. |
BTW, Version 6.2 of the book was released an hour or two ago
|
Mark M. |
if you haven't done so, be sure to download your copy
|
Craig D. |
Cool! I will definitely get it.
|
Craig D. |
Thanks
|
Nov 17 | 10:00 AM |
Mark M. |
and that's a wrap for today's chat
|
Mark M. |
the next chat is next Monday, at 7:30pm US Eastern
|
Mark M. |
have a pleasant day!
|
Craig D. |
thank you.
|
Craig D. | has left the room |
Mark M. | turned off guest access |