I’m writing code to display notifications to the user at specific times (just like the Google Calendar app).
I hence created :
- a
BroadcastReceiverthat listens toBOOT_COMPLETED, upon reception it sets an alarm in one minute in order to not overload the device when it is still loading stuff; - a
BroadcastReceiverthat listens to alarms: the first set one minute afterBOOT_COMPLETED, and the next at the next appointment (like in Google Calendar)
So, typically:
BOOT_COMPLETED=> launch alarm with a one minute delay- One minute later => the Receiver sets another alarm for the next appointment
- Several minutes/hours/days later, the alarm goes off => the Receiver displays a status bar notification
Which means that the status bar notifications are launched from the BroadcastReceiver.
I have read in the doc that they should be launched from Activities or Services : https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Basics
I’m asking for best practice here. Should I create a Service that will be launched by the BroadcastReceiver, and which only purpose will be to launch the status bar notification? My code is working, I just want to create clean code as suggested by Google.
You can add a
Notificationfrom aBroadcastReceiver, AFAIK. That should be fairly fast. IfStrictModecomplains about it, then it might be worth worrying about — otherwise, you should be OK.