Topper Harley
Topper Harley

Reputation: 12384

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 :

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.

Upvotes: 7

Views: 2137

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007296

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.

Upvotes: 1

Related Questions