Nouvel Travay Nouvel Travay - 2 years ago 101
Android Question

Showing a Snackbar from inside a Service

When showing a SnackBar from inside an Activity, I have the rootView available. But I need to show a SnackBar from inside a Service, where I do not have a View available. How might I accomplish this?

As backstory: an activity starts a service to do a task. The Service needs to show a SnackBar depending on situations. I don’t want to Bind to the Service just for that. So how might I accomplish this? Normally I could show a Toast, but I need the user to be able to read the message and confirm so.

Answer Source

The Snackbar needs a View to be displayed, so if you want to show snackbars on your app depending on the state of your Service you'll have to either bind it to your Activity or broadcast a message through the LocalBroadcastManager and show a message on your View.

I don't think there's any other way around it, you'll have to communicate with your Activity or Fragment somehow.

Snackbars are not like Toasts that only need a context, so if you want to display it out of your app, I believe you can't with the class provided by Android.

As from the design guidelines:


Snackbars appear above most elements on screen, and they are equal in elevation to the floating action button. However, they are lower in elevation than dialogs, bottom sheets, and navigation drawers.

It's not explicit, but you can reach the conclusion that it'll only display inside your app views. So, again, you'll have to communicate with your visible view somehow.

Snippets on broadcasting a message:

Sender (on your Service)

private void doSendBroadcast(String message) {
    Intent it = new Intent("EVENT_SNACKBAR");

    if (!TextUtils.isEmpty(message))


Receiver (on your Activity)

private BroadcastReceiver mMessageReceiver = null;

public void onCreate(Bundle savedInstanceState) {
    // Other stuff.

    mMessageReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            // Do something

public void onResume() {

    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("EVENT_SNACKBAR"));

More on bound services here.

And about LocalBroadcastManager here on this question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download