How to get Phone Network Information in WP7 Mango via code
published on: 5/27/2011 | Views: N/A | Tags: Mango
by WindowsPhoneGeek
In this post I am going to talk about how to get device network information in a Windows Phone 7.1(Mango) application.
With the Mango release your applications can now access information about the network and network interfaces. You can determine the mobile operator and network capabilities of a user's phone, and you can get and set connection preferences and requirements.
NOTE: Before Windows Phone 7.1 the only way to get network information was through the NetworkInterface Class which allows you to get only limited details, such as, whether there is a network connection available and if it is a cellular or WiFi connection. It was not possible to access details like the mobile operator name or id or whether the device is in roaming.
NOTE: Before we begin make sure that you have installed the Windows Phone Developer Tools 7.1 Beta(Mango).
When you create a Windows Phone app, you might want to know the network capabilities of the user's phone. You can determine the network capabilities by using the properties of the DeviceNetworkInformation class. Because the properties are static, you do not have to create an instance of the class first; you can just access the properties directly. You can use the following properties:
- CellularMobileOperator
- IsNetworkAvailable
- IsCellularDataEnabled
- IsCellularDataRoamingEnabled
- IsWiFiEnabled
Here are the answers to some questions that we received regarding the new Mango update:
1. How to get the Mobile Operator Name
You can get the mobile operator name through the CellularMobileOperator property of DeviceNetworkInformation:
<TextBlock x:Name="tbCellOperator" FontSize="25"/>
this.tbCellOperator.Text ="Mobile Operator: "+ DeviceNetworkInformation.CellularMobileOperator;
2. How to determine whether Roming is available
You can determine whether Roming is available or not by using the IsCellularDataRoamingEnabled property of DeviceNetworkInformation:
<TextBlock x:Name="tbRoaming" FontSize="25"/>
this.tbRoaming.Text = "Roming Available: " + DeviceNetworkInformation.IsCellularDataRoamingEnabled;
3. How to determine whether Cellular Data is available
You can determine whether Cellular Data is available or not by using the IsCellularDataEnabled property of DeviceNetworkInformation:
<TextBlock x:Name="tbCellular" FontSize="25"/>
this.tbCellular.Text = "Cellular Data Available: " + DeviceNetworkInformation.IsCellularDataEnabled;
4. How to determine whether Network is available
You can determine whether Network is available or not by using the IsNetworkAvailable property of DeviceNetworkInformation:
<TextBlock x:Name="tbNetAvailable" FontSize="25"/>
this.tbNetAvailable.Text = "Network Available: " + DeviceNetworkInformation.IsNetworkAvailable;
5. How to determine whether Wi Fi is available
You can determine whether Wi Fi is available or not by using the IsWiFiEnabled property of DeviceNetworkInformation:
<TextBlock x:Name="tbWiFi" FontSize="25"/>
this.tbWiFi.Text = "WiFi Available: " + DeviceNetworkInformation.IsWiFiEnabled;
6. How to detect when the availability of the network changes
You can use the NetworkAvailabilityChanged event which occurs when the availability of the network changes:
DeviceNetworkInformation.NetworkAvailabilityChanged += new EventHandler<NetworkNotificationEventArgs>(DeviceNetworkInformation_NetworkAvailabilityChanged);
...
void DeviceNetworkInformation_NetworkAvailabilityChanged(object sender, NetworkNotificationEventArgs e)
{
//add some code here
}
Reference: MSDN Documentation
That was all about how to get device network information in Windows Phone Developer Tools 7.1 Beta(Mango). Here is the full source code:
I hope that the post was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Samples
posted by: TP on 5/27/2011 12:08:43 PM
Thank you for answering my questions and for providing these samples. Cheers!
Mango
posted by: Peter Smith on 5/27/2011 12:35:02 PM
Really pretty cool stuff comes with Mango..
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
