Over a million developers have joined DZone.

Android to Windows Phone 8: Manage Network Usage

DZone's Guide to

Android to Windows Phone 8: Manage Network Usage

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

It’s more than likely that a Windows Phone app will need to work with data that resides on the Internet. For example, an app may aggregate data from various news sources using RSS feeds. Consuming this type of data in a Windows Phone app is very similar to consuming it in an Android app. In this post I’ll show you how to manage network usage.

It’s important to consider how your Windows Phone app will access network resources once it has the capability to do so. If your app is going to perform a large number of network operations or transfer large amounts of data (e.g. audio/video streaming) you should allow the user to control when these operations are performed.

In a Windows Phone app you can put the user in control over network usage in four steps. First, create an enumeration representing various states of connectivity:

public enum ConnectionState

Second, create a method that leverages the NetworkInterface class to determine if the device the app is running on is connected to a network and, if the device is connected, what kind of network the device is on. The method could look like the following:

private ConnectionState GetConnectionState()
 switch (Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType)
  case NetworkInterfaceType.None:
   return ConnectionState.NotConnected;

  case NetworkInterfaceType.Ethernet:
   return ConnectionState.Ethernet;

  case NetworkInterfaceType.Wireless80211:
   return ConnectionState.WiFi;

  case NetworkInterfaceType.MobileBroadbandCdma:
  case NetworkInterfaceType.MobileBroadbandGsm:
   return ConnectionState.Cellular;

   return ConnectionState.Unknown;

Third, add a method to your class that will perform an operation based on the connection state:

private void UpdateActivityBasedOnConnectionState()
 var connectionState = GetConnectionState();
 //Perform some action based on state (e.g. start/stop streaming media, prompt user, etc.)

Finally, in the constructor of your class add an event handler for theNetworkChange.NetworkAddressChanged event. This event fires when the network address changes for a connection. This will allow you to know if a connection has been lost, or if the device has switched to a different type of connection.

public NetworkManager()
 NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;

private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
Android tip
To obtain information about the network(s) a device is connected to in Android you would need to declare the following manifest permission:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

You would then use the ConnectivityManager and NetworkInfo classes to determine what type of network the device is connected to. The following code shows how to determine if a device is connected to a Wi-Fi or mobile network:

private boolean IsWifiConnection(){
 ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

 NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

 return networkInfo.isConnected();

private boolean IsMobileConnection(){
 ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

 NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

 return networkInfo.isConnected()

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.


Published at DZone with permission of Adam Grocholski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}