If we are busy transferring data to or from our device, we need to respond appropriately if the network goes down or if it changes from Wi-Fi to mobile data for some reason.
In order to receive events from the ConnectivityManager
instance, we register a BroadcastReceiver
instance with the current activity or service:
When we want to access the state of the network, we have to request permission from Android first:
[assembly: UsesPermission( Manifest.Permission.AccessNetworkState)]
After obtaining permission, we create an instance of
BroadcastReceiver
that will handle network events:private class NetworkReceiver : BroadcastReceiver { public override void OnReceive( Context context, Intent intent) { } }
In the
OnReceive()
method, we handle the network events:var manager = ConnectivityManager.FromContext(context); var networkInfo = manager.ActiveNetworkInfo; if (networkInfo != null && networkInfo.IsConnected) { // we are connected...