javaandroidandroid-broadcastreceivernotificationmanager

Why does getCurrentInterruptionFilter BroadcastReceiver work from Quick Settings but not from Settings?


I have the following Broadcast Receiver to get hold of the changes in my Interruption Filter.

public class DndBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED);
        if (NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED.equals(intent.getAction())) {
            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            assert mNotificationManager != null;
            if (mNotificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_NONE) {
                System.out.println("None");
            } else if (mNotificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_ALARMS) {
                System.out.println("Alarms");
            } else if (mNotificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_ALL) {
                System.out.println("All");
            } else if (mNotificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_PRIORITY) {
                System.out.println("Priority");
            } else if (mNotificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_UNKNOWN) {
                System.out.println("Unknown");
            }
        }
    }
}

This works really well when I toggle the Quick Settings DND button, but when I turn DND on or off using the button in the actual Settings panel, no change is detected. Is there any obvious reason why this might be?


Solution

  • Ok, this is because my Broadcast Receiver only works when my app is in the foreground. So quick settings works, but not settings because I am on the settings screen.