Search code examples
javaandroidbroadcastreceiverandroid-widget

Can a homescreen widget BroadcastReceiver listen for phone events?


I am trying to update my homescreen widget when there is a Do not Disturb change. Because WidgetProvider is a BroadcastReceiver in itself, I thought it would be simple. But I am not seeing any Toast when I switch Dnd on or off. Does it mean that the WidgetProvider can only send broadcasts but not receive them?

Here is my code:

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);

        String action = intent.getAction();

        assert action != null;
        if (action.equals(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED)) {
            System.out.println("Success!");
        }
     }

Solution

  • You do not show how you are registering for that broadcast. If it is in the manifest, the broadcast that you are trying to receive is not listed on the implicit broadcast exceptions "whitelist", so you may not receive it on Android 8.0+. The only way that you would be able to receive that broadcast on Android 8.0+ is if you have a foreground service running all the time, and that has costs.

    Also:

    • I am not familiar with that specific broadcast and so I am uncertain if it is sent on a DND change (though it certainly seems plausible)

    • You are not showing a Toast; instead, you are printing a message to stdout, which should get redirected to Logcat on Android