Showing a Snackbar from inside a Service

Nouvel Travay picture Nouvel Travay · Jan 18, 2016 · Viewed 10.4k times · Source

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

Mauker picture Mauker · Jan 18, 2016

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:

Placement

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))
        it.putExtra(EXTRA_RETURN_MESSAGE,message);

    LocalBroadcastManager.getInstance(mContext).sendBroadcast(it);
}

Receiver (on your Activity)

private BroadcastReceiver mMessageReceiver = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Other stuff.

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

@Override
public void onResume() {
    super.onResume();

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

More on bound services here.

And about LocalBroadcastManager here on this question.

Update: You could also make use of an EventBus to communicate with your visible view, as it works on a Publisher/Subscriber fashion. You could even make use of the concept of Sticky events to make sure the Snackbar will be displayed once the app is visible again.

Take a look at this answer of mine on how to use the Event Bus.