
One of the luxuries we as developers now have is the ability to pretty much assume that our users (and apps) will be connected to the Internet. Of course we need to plan for some offline scenarios, but for the most part we know a connection will be present. With that said, I believe we have a responsibility to our users to respect the type of connection they’re on. What do I mean by “type of connection”? I’m talking about the ability for a user to flag a connection as “Metered”. This allows the user to let apps know that “Hey, this connection is costing me money, please be careful if you happen to be using it.” For example, in my Radio Soma app I let the users select how they want the app to behave when a metered connection is in use via the following page:

In this case, when the user wants to stream music over a metered connection the app will prompt them first to confirm they want to continue streaming music. Users appreciate having this kind of control and it shows you’ve put some thought into your app and that you care about your user base.
Setting this type of functionality up is easy to do, so I thought I’d spend the remainder of this post showing you how I did it for Radio Soma.
The first thing I did was to create the following enumeration for potential connection state values:
Next I created a new class and added three events to it that can be fired when various connection changes occur:
I then added two fields to the class to keep track of the last know connection state and to provide access to roaming settings.
Following the fields I added three properties to the class. The first just me to use the class as a singleton; which is all that I need as I don’t require multiple instances of this class floating around in my application and causing chaos. The MeteredConnectionPrompt property is used to keep track of whether or not the user wants to be prompted when streaming music over a metered connection. Lastly, the IsConnected property lets the app know whether or not an internet connection is present. You’ll undoubtedly notice the empty catch{} block. I found that every now and then an exception would be thrown if a connection wasn’t present. I simply chose to bury the exception and return a value of false in this case. Not my proudest moment, but it works.
I needed a couple of methods for quickly accessing data stored in roaming settings. While I could have just used these inline in my Load() and Save() methods coming a little later, I prefer separate methods as it allows me to easily add and remove roaming settings within the class.
Next are the Load() and Save() methods which interact with roaming settings to retrieve and persist the value of the MeteredConnectionPrompt property.
Now comes the most important method, GetNetworkInformation(). In this method I determine if a connection is present, and, if so, what type of connection it is, raising the appropriate events along the way.
Finally, I add the constructor for the class. In this case it’s marked as private because I’m using the class as a singleton. I also have an event handler for the NetworkStatusChanged event from the NetworkInformation class.
You can view the full source here: