Snackbar needs a View to display, so if you want to show snacks in your application depending on the status of your Service , you will either have to bind it to Activity or send a message through LocalBroadcastManager and display the message on View .
I donβt think there is any other way there, you will have to somehow communicate with your Activity or Fragment .
Snacks are not like toasts that only need context , so if you want to display it from your application, I believe that you cannot with the class provided by Android.
As from the design rules :
Accommodation
Snowflakes appear above most of the elements on the screen, and they are equal in elevation to the float button. However, they are lower in height than dialogs, bottom sheets, and navigation boxes.
This is not explicit, but you may come to the conclusion that it will only appear in your applications. So, again, you will need to somehow communicate with your visible .
Fragments of the broadcast message:
Sender (at 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); }
Recipient (in 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")); }
Learn more about related services here .
And about LocalBroadcastManager here for that matter .
Update: You can also use EventBus to communicate with your visible view, since it works on the Publisher / Subscriber module. You can even use the Attached Events concept to make sure that the Snackbar will be displayed after re-viewing the application.
Take a look at this answer on how to use Event Bus.