Windows Phone 7 (Mango) Tutorial - 27 - How to Detect whether the Network is Available?
Sometime we need to know whether Network connectivity is available or not from our application. In Windows Phone 7, we have the required API exposed for the developers to detect the status of the network connection and take respective step based on that.
So how to detect it? In this small tip of the tutorial series, we will learn about the API and we will also see how this can be done using a small demo which includes code too. Read to know more about it.
Index - Windows Phone 7 (Mango) Tutorial
Know about the APIs
Let's discuss about the API first. NetworkInterface class present inside the Microsoft.Phone.Net.NetworkInformation namespace is responsible to inform you whether the network connection is available or not. NetworkInterface class has a method called GetIsNetworkAvailable(). Once called it will return boolean value true or false based on the current network availability.
Microsoft.Phone.Net.NetworkInformation.NetworkInterface class inherits from the base class System.Net.NetworkInformation.NetworkInterface class. Find the complete Meta Data of the classes here:
Modifying the Code
It's time to create a small demo application to showcase the same. This will clarify many things. Let's create a new Windows Phone 7 application project. Now inside the MainPage.xaml, we will create a DependencyProperty "NetworkAvailability" of type string which will return the text string displaying whether the network is available or not.
Inside the constructor, we will call the NetworkInterface.GetIsNetworkAvailable() method and populate the property properly. Need assistance? Find the code below for your reference:
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
namespace NetworkAvailabilityDemo
{
public partial class MainPage : PhoneApplicationPage
{
public string NetworkAvailability
{
get { return (string)GetValue(NetProperty); }
set { SetValue(NetProperty, value); }
}
public static readonly DependencyProperty NetProperty =
DependencyProperty.Register("NetworkAvailability",
typeof(string),
typeof(MainPage),
new PropertyMetadata(string.Empty));
// Constructor
public MainPage()
{
InitializeComponent();
NetworkAvailability = NetworkInterface.GetIsNetworkAvailable() ?
"available" : "not available";
}
}
}
Now move onto the XAML page and add a TextBlock inside the ContentPanel Grid control. Bind the text properly with the DependencyProperty that we exposed. Find the code here in case you need reference:
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="{Binding NetworkAvailability, ElementName=phonePage, StringFormat='Internet Connection is {0}'}" FontSize="30"/>
</Grid>
Remember to use the ElementName attribute to the name of the class while binding. We named the XAML page as "phonePage" and hence the ElementName is set to phonePage to retrieve the proper bindings.
Demo
Let's see the demo of it. Run the application inside the Phone Emulator. Once the application runs, it will show that the Internet Connection is Available. Here, the API returns True and based on this, our code sets the value in the screen.
Find the screenshot of the running demo here:
Hope this gave you some basic understandings about the API. Now you will be able to use it in your application to check whether the network connectivity is available to your phone and depending upon the returned value you will be able to take action based on your business requirement. Let me know, if you have any feedback. Enjoy reading my articles. Happy Coding. Cheers.

Also, don't forget to share your views and/or feedback in the comment section below.
Hi,
ReplyDeleteDid you try to call this method on a slow network? I read many articles that it can take up to 1 minute and to call this method, it should be done in a background thread.
Anonymous
Hi,
ReplyDeleteI also read many articles too regarding that this method should be done in a background thread especially if you run into a situation where the network is really slow. Kunal, do you have any thoughts regarding this issue?
Thanks
Hi,
ReplyDeleteI didn't hear about this. I will check if I can get more information on it. BTW, thanks for informing.
From http://msdn.microsoft.com/en-us/library/ff967560%28v=vs.92%29.aspx
ReplyDeleteNetwork Information
When Windows Phone starts, it can take up to 20 seconds for the first call to the M:System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable to return. This is because Windows Phone initially enumerates all the available network connections. For ways to call the GetIsNetworkAvailable method in the background, see the section Background Threads and Asynchronous Programming later in this topic.
If we talk about the emulator this function will always return true!!
ReplyDeleteSo do we have any function or method to know the network connectivity in emulator also?
Hello Sir,
ReplyDeletei am developing wp7 app which is totally depend on webservice. I want to add NetworkStateChanged event in my app so that i can detect whenever connectivity losses or gains at any page throughout in my app. How should i achieve this? please guide me.