Display Snackbar from inside a service

When you show the SnackBar from inside the Activity, I have a rootView. But I need to show SnackBar inside a service where I don't have View. How can i do this?

As a background: an action starts a service to complete a task. The service should show the SnackBar as appropriate. I do not want to become attached to the service just for this. So how can I do this? Usually I can show Toast, but I need the user to read the message and acknowledge it.

+6
source share
4 answers

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.

+9
source

You can always send the broadcast through LocalBroadcastManager in your Service

 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("SERVICE_DID_SOMETHING")); 

And then in Activity use BroadcastReceiver to show Snackbar

 private final ServiceReceiver _serviceReceiver = new ServiceReceiver(); @Override protected void onResume() { super.onResume(); final IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("SERVICE_DID_SOMETHING"); LocalBroadcastManager.getInstance(this).registerReceiver(_serviceReceiver, intentFilter); } @Override protected void onPause() { super.onPause(); try { LocalBroadcastManager.getInstance(this).unregisterReceiver(_serviceReceiver); } catch (Exception ex) { Log.e(TAG, "Error unregistering ServiceReceiver", ex); } } // region ServiceReceiver private class ServiceReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: Show your Snackbar here... } } // endregion 

You can use the sent Intent to transfer additional data, if necessary, and then pull it into BroadcastReceiver.onReceive(Context context, Intent intent)

+2
source

This cannot be done using the standard SnackBar provided by the SDK, as it always requires a view.

To serve your purpose, you can use some external library that mimics the SnackBar, like this one

+1
source

How to create a Snackbar with an application context that appears in several actions: fooobar.com/questions/384834 / ...

0
source

All Articles