Mobile Zone is brought to you in partnership with:

Hey there! My name is Adam, and I'm a Technical Evangelist at Microsoft where I spend time focusing on Windows, Windows Phone, and Windows Azure. Adam is a DZone MVB and is not an employee of DZone and has posted 30 posts at DZone. You can read more from them at their website. View Full User Profile

Android to Windows Phone 8: Manage Network Usage

06.26.2013
| 3751 views |
  • submit to reddit

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
{
 NotConnected,
 WiFi,
 Ethernet, 
 Cellular,
 Unknown
}

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;

  default:
   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()
{
 UpdateActivityBasedOnConnectionState();
 NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
}

private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
 UpdateActivityBasedOnConnectionState();
}
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()
}


Published at DZone with permission of Adam Grocholski, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)