Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Snackbars in a BroadcastReceiver?

Snackbars provide lightweight feedback about an operation by showing a brief message at the bottom of the screen. Snackbars can contain an action.

Android also provides a toast, primarily used for system messaging. Toasts are similar to snackbars but do not contain actions and cannot be swiped off screen.

My question

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class TestReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        Toast.makeText(context, "status", Toast.LENGTH_LONG).show();
    }
}

Is it posible to show a Snackbar in a BroadcastReceiver like Toast?

like image 767
praj Avatar asked Dec 04 '22 03:12

praj


1 Answers

is it posible to show snakbars in BroadcastReceiver like Toast?

A BroadcastReceiver registered by an activity or fragment, via registerReceiver(), could ask the activity or fragment to show a snackbar.

A manifest-registered BroadcastReceiver has no UI, and hence it has no place to show a snackbar. What it could do is post an event on an in-process event bus (e.g., LocalBroadcastManager, greenrobot's EventBus, Square's Otto), to let whatever UI of yours that is in the foreground know that a broadcast was received. If the UI layer receives the message, that activity or fragment can show a snackbar. If the event bus event was not picked up, you can perhaps show a Notification as a fallback, if appropriate.

like image 128
CommonsWare Avatar answered Dec 31 '22 14:12

CommonsWare