Home > Software engineering >  .Net Maui BroadcastReceiver does not receive anything
.Net Maui BroadcastReceiver does not receive anything

Time:09-04

I implemented an BroadcastReceiver for Android-Platform to detect whenether the Devices Battery is being Charged or not. Unfortunately, it doesn't seem to work on my Device which has Android 10 installed (Android 10 is my minimum requirement for the App).
I need this BroadcastReceiver to be triggered even if the App is not running. Therefore an Implicit broadcast would be an excellent choice instead of register an BroadcastReceiver while the App is running.

Permissions set within "AndoirdManifest.xml"

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BATTERY_STATS" />

My PowerConnectedBroadcastReceiver looks like this:

    [BroadcastReceiver(Enabled = true, Exported = true)]
    [IntentFilter(new[] { Intent.ActionPowerConnected, Intent.ActionPowerDisconnected, Intent.ActionDockEvent, Intent.ActionBatteryChanged }, Priority = (int)IntentFilterPriority.HighPriority)]
    public class PowerConnectedBroadcastReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            Console.WriteLine($"PowerConnectedBroadcastReceiver received an intent: {intent}");
        }
    }  

What am I doing wrong?
Any Kind of advise would be appreciated.

CodePudding user response:

From Broadcast Receivers docs

Apps that target Android 8.0 (API level 26) or higher may not statically register for an implicit broadcast. Apps may still statically register for an explicit broadcast. There is a small list of implicit broadcasts that are exempt from this restriction. These exceptions are described in the Implicit Broadcast Exceptions guide in the Android documentation. Apps that are interested in implicit broadcasts must do so dynamically using the RegisterReceiver method. This is described next.

Dynamic registration

 [BroadcastReceiver(Enabled = true, Exported = true)]
    public class PowerConnectedBroadcastReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            Console.WriteLine($"PowerConnectedBroadcastReceiver received an intent: {intent}");
        }
    }
PowerConnectedBroadcastReceiver receiver;
IntentFilter intentFilter;

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        receiver = new();
        intentFilter = new(Intent.ActionPowerConnected);

        intentFilter.AddAction(Intent.ActionPowerDisconnected);
        intentFilter.AddAction(Intent.ActionDockEvent);
        intentFilter.AddAction(Intent.ActionBatteryChanged);
        intentFilter.Priority = (int)IntentFilterPriority.HighPriority;
    }

    protected override void OnResume()
    {
        base.OnResume();
        RegisterReceiver(receiver, intentFilter);
    }

    protected override void OnPause()
    {
        UnregisterReceiver(receiver);
        base.OnPause();
    }

Edit

As the broadcast receiver is managed inside the activity it has the life cycle of the activity, if you want it to keep running for some reasons even if the app is not running, then i believe you should register/unregister it inside a foreground service.

Related questions:

Android keep BroadcastReceiver in background

How to keep broadcast receiver running in background?

Extra

Failing on un-register the broadcast receiver when the activity is terminated, will result in a leak. UnregisterReceiver() is called in OnPause rather than OnDestry() because the latter is not guaranteed to be called.

Android Activity onDestroy() is not always called and if called only part of the code is executed

  • Related