Currently created mobile applications can not run without Internet connection. We can provide unlimited number of usage examples of data transmission in applications. We are only limited by our imagination.
By implementing this type of solution in applications, we very often forget about small thing, that network check query can be performed. This tiny thing is to check, if the phone has access to the network. Getting positive information about access to the network does not guarantee us, that we will have Internet connection. This information may be used to support situations that network is unavailable.
Very often, if network availability is not checked, handling this situation is made by the exception. This is not exactly correct, considering the small cost of adding network check. To do this, we need simply call one method:
using System.Net.NetworkInformation; bool isAvailable = NetworkInterface.GetIsNetworkAvailable(); MessageBox.Show(isAvailable.ToString());
The static method NetworkInterface.GetIsNetworkAvailable() allows to obtain information, whether your phone has access to any network. When you are using this method you should remember about one little thing, moreover, as well as in a situation to use a network connection – the phone needs some time, to log on to the network. This information is particularly important, if you have only access to WiFi. In a situation, when you run an application immediately after leaving the phone from sleep mode, you will get the message, that there is no network available. What may prove to be untrue because the phone could not even log on to the available WiFi.
In addition to information about, if the network is available we also have access to a type of a network that is connected to our phone. This information can be very helpful, if we want to build a very responsive user interface in applications that use intensive network connection. When we detect a connection to the slower type of network you can disable certain features – for example, play the video in low resolution on a slow connection, in the case of fast – in high resolution.
In order to check what connection is available, we need to check the property:
This property returns the following values:
- Wireless80211 – Connection to WiFi,
- Ethernet – Connection via cable USB connected to the computer,
- None – No network.