Forum Xamarin.Android

Cannot get BroadcastReceiver to work with ConnectivityAction

JimBordenJimBorden USMember ✭✭

I've been slaving away at this but hours later still nothing. Every single tutorial puts together a small example and says "this is all you need to do." But when I do it, OnReceive is never called! Here is the situation:

I am developing a library which will subscribe to network change events. I have a class which will handle the life of the receiver. When "start" is called, this happens:

```c#
var receiver = Interlocked.CompareExchange(ref _receiver, new AndroidNetworkChangeReceiver(),
null);
if (receiver != null) {
return; // We only need one handler.
}

_receiver.Callback = InvokeNetworkChangeEvent;
var intent = new IntentFilter();
intent.AddAction(ConnectivityManager.ConnectivityAction);
intent.AddAction(WifiManager.WifiStateChangedAction); // Tried with and without this
Application.Context.RegisterReceiver(_receiver, intent);


I have these two lines compiled into the assembly

c# [assembly: UsesPermission(Manifest.Permission.AccessNetworkState)] [assembly: UsesPermission(Manifest.Permission.AccessWifiState)]
OnReceive is never called...what else can I do? There are no errors or anything, just silence...

On the other hand, NetworkChange.NetworkAddressChanged is called when the network is enabled but not when it is disabled and so it is only half useful. Please let me know how to overcome this.

Sign In or Register to comment.