Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display status bar notification from a BroadcastReceiver

I'm writing code to display notifications to the user at specific times (just like the Google Calendar app).

I hence created :

  • a BroadcastReceiver that listens to BOOT_COMPLETED, upon reception it sets an alarm in one minute in order to not overload the device when it is still loading stuff;
  • a BroadcastReceiver that listens to alarms: the first set one minute after BOOT_COMPLETED, and the next at the next appointment (like in Google Calendar)

So, typically:

  1. BOOT_COMPLETED => launch alarm with a one minute delay
  2. One minute later => the Receiver sets another alarm for the next appointment
  3. 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.

like image 774
Benoit Duffez Avatar asked Feb 26 '11 14:02

Benoit Duffez


1 Answers

You can add a Notification from a BroadcastReceiver, AFAIK. That should be fairly fast. If StrictMode complains about it, then it might be worth worrying about -- otherwise, you should be OK.

like image 156
CommonsWare Avatar answered Sep 22 '22 01:09

CommonsWare