Obtaining a network state

When we want to send or request data from a remote source, such as the Internet, we need to make sure that we can access the network.

How to do it...

In order to access the state of the network, we make use of the ConnectivityManager instance:

  1. When we want to access the state of the network, we have to request permission from Android first:
    [assembly: UsesPermission(
      Manifest.Permission.AccessNetworkState)]
  2. Now that we have permission, if we want the network state, we need to get hold of the ConnectivityManager instance through the static FromContext() method:
    var manager = ConnectivityManager.FromContext(this);
  3. Once we have the manager, we can get hold of the current state:
    var networkInfo = manager.ActiveNetworkInfo;
  4. If the ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.